JavaScript Tutorial - 4

If you visited the White House web site in the early days, you may have noticed that the greeting (and the picture as well) depended on the time of day.

Task 8: Produce a greeting similar to the one at the top of this page. Check.

Task 9: Create a page that gives a different message when it's the weekend and when it's not. Check.

Task 10: Let your creative juices flow - extend the idea of Task 9 to produce a different message for each day of the week.
And now onto another one of the BIG ideas in computing. Imagine the houses in a street (humour me) - all different - one may even be a shop. And yet we can easily identify each one by a number. We can say things like:

  • House number 7 is brick.
  • The Clintons live in house number 1600.
  • Number 201 is vacant.
Riveting stuff eh? In computer speak this is known as an array.
Our work with dates provides a good example:
  • day 0 is Sunday
  • day 1 is Monday
  • day 2 is Tuesday
  • day 3 is Wednesday
  • day 4 is Thursday
  • day 5 is Friday
  • day 6 is Saturday
And this is how we write it in JavaScript: var day=new Array() day[0]="Sunday" day[1]="Monday" day[2]="Tuesday" day[3]="Wednesday" day[4]="Thursday" day[5]="Friday" day[6]="Saturday" Remember Task 6? Check out how much easier it is if we use an array: <html> <head> <title>Today</title> </head> <body> <script> var day=new Array() day[0]="Sunday" day[1]="Monday" day[2]="Tuesday" day[3]="Wednesday" day[4]="Thursday" day[5]="Friday" day[6]="Saturday" var now=new Date() var day_number=now.getDay() document.write(day[day_number]) </script> </body> </html> Task 11: Convince yourself that you follow the example by rewriting the script to display what month it is. Check.

Challenge 5: Put it altogether and create a page that displays the date as attractively as possible - like this:
 Friday, January 1st 99
btw my solution doesn't use an if statement :o)

HOME Table of Contents Previous Next