...

View Full Version : Need Help With Basic JS Homework! (Date & Custom Objects)



JSNooblet
05-26-2009, 11:20 PM
Ok so here is the assignment:

Create a Web page containing JavaScript statements and functions that perform the following:

* Today's date should be displayed in the following format: Today is: mm/dd/yyyy
* If the day is Monday, Tuesday, or Wednesday, display a message that says "Have a nice week!
* If the day is Thursday or Friday, display a message that says "The weekend is almost here!"
* If the day is Saturday or Sunday, display a message that says "Enjoy the weekend!"
* If the date is after the 25th of a month, display a message that says "Next month is just around the corner!"

Example:

Today is 12/26/08
The weekend is almost here!
Next month is just around the corner!
--------------------------------------------------------------------------

Here is what I have so far:


<html>

<head>

<title>

</title>

</head>

<body>

<script type="text/javascript">
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;//January is 0!
var yyyy = today.getFullYear();
if(dd<10){dd='0'+dd}
if(mm<10){mm='0'+mm}
onload = function(){
document.write('Today is: '+mm+'/'+dd+'/'+yyyy)
}
</script>

</body>

</html>

As you can see I found out how to display the date, but as for having certain text appear based on that date, I am completely lost.

Thanks ahead for any help, as this is due today and I am stressing!

Eldarrion
05-26-2009, 11:24 PM
Have a look at the following site:

http://www.w3schools.com/js/js_obj_date.asp

Specifically you might be interested in the getDay() function. If you still have questions afterwards... don't hesitate to ask.

JSNooblet
05-26-2009, 11:27 PM
Thanks for taking the time to help me out. I will check that out right now.

*Edit: Here is the final code based on indirect help from a classmate.


<html>

<head>

<title>
Test!
</title>

</head>

<body>

<script type="text/javascript">
/* <![CDATA{ */
var today=new Date();
var dd=today.getDate();
var mm=today.getMonth()+1;
var yyyy=today.getFullYear();
document.write("Today is " + mm + "/" + dd + "/" + yyyy);

var today = new Date();
var currentDay = today.getDay();
if (currentDay ==1 || currentDay ==2 || currentDay ==3)
document.write("<p>Have a nice week!");
else if (currentDay == 4 || currentDay == 5)
document.write("<p>The weekend is almost here!");
else if (currentDay == 6 || currentDay == 0)
document.write("<p>Enjoy the weekend!");

if (dd == 26 ||27 || 28 || 29 || 30 || 31)
document.write("<p>Next month is just around the corner!");

/* ]] */
</script>

</body>

</html>

Old Pedant
05-27-2009, 12:06 AM
No, that code is *WRONG*.

This line is NOT WORKING:


if (dd == 26 ||27 || 28 || 29 || 30 || 31)
document.write("<p>Next month is just around the corner!");


To see that I am right, add in the line

dd = 1;
just before that (in other words, pretend today is the first of the month).

HINT: Don't test for the individual values.

If you are interested in improving your coding skills, consider these:

(1) Why do you need (or want) to check for currentDay being 6 or 0? If it is none of the other values, it must be one of those two.

(2) Instead of using if...else if... consider using switch ( currentDay ) instead. See how much that makes the code clearer and simpler.

For extra credit, have your "next month is..." message appear on only the last 5 days of the month, NO MATTER WHAT THE MONTH. (In other words, for example, in February 2010, it should appear on the 24th through 28th.)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum