Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Location
    Wisconsin
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question I need help with date object model

    Hey guys, this is an assignment for school. I have most of the code done but I cant figure out how to completely finish one part of the assignment. I bolded the part in which i'm having trouble with.

    Heres the Directions per the Instructor:

    Create a new HTML document that creates a listing of the days and date for the current month.

    1. Create a new array, named monthArray that holds the names of the all of the months in a year, i.e. January, February…

    2. Create a new array, named weekDaysArray that holds all of the days of the week, i.e. Sun, Mon, Tue,….

    3. Get today’s date by using the Date object, without initializing a value for the object and save it in a variable named todaysDate.
    a. Using the Date object methods, break up the value of todaysDate into variables that hold each of the pieces of the date, by assigning the values to the following variables: (see page 169 in text)
    i. currYr = year
    ii. currMon = month as an integer
    iii. currDate = day of the month
    iv. currDay = the day of the week as an integer

    4. Use a document.write statement display the name of the month followed by the 4 digit year. (i.e. October 2011)
    a. Use currMon as the index to get the name of the month from the monthArray created in step 1.
    b. Use the variable currYr created in step 3.
    c. Include a blank line after this displayed line

    5. Determine the name of the first day of the current month.
    a. Using the correct Date Object method, get the integer for the day of the week for the first day of the current month. (hint: you will need to pass the year, month and the day value of 1 to the Date Object to get this number)


    6. Use a document.write statement to display every day of the current month on a new line in the format of the name of the day followed by the integer for the date.
    a. Use a while loop to keep track of the number of days written for the month.
    b. Use a for loop to write out the days of the week from the array created in step 2 under the name of the current month and year.

    Example output:
    October 2011
    Sat 1
    Sun 2
    Mon 3, etc….until all 31 days are displayed.

    My Code:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    	
    	<head>
    	
    		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    	
    		<title>Assignment 9</title>
    		
    		<script type="text/javascript">
    			
    			
    			
    
    			var monthArray = new Array ("January","February","March","April","May","June","July","August","September","October","November","December");
    			
    			var weekDaysArray = new Array ("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
    			
    				var todaysDate = new Date();
    				var currYr = todaysDate.getFullYear();
    				var currMon = todaysDate.getMonth();
    				var currDate = todaysDate.getDate();
    				var currDay = todaysDate.getDay(); 
    			
    			document.write(monthArray[currMon] + " " +currYr);
    			document.write("<br />");
    			document.write(weekDaysArray[currDay] + "<br />");
    			
    			
    			var count = 0;
    			while (count < 31)
    			{
    				count++;
    			}
      				
      			for(var count = 0 ; count < 31 ; count++)
    			{
    				document.write(weekDaysArray[count]+ "<br />");
    			}
    			
    			
    		</script>
    	
    	</head>
    	
    	<body>
    		
    	</body>
    	
    </html>

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    As this is homework I can only give you some hints

    1. Get the current date
    2. Invoke the methods to get the date parts for current month and current year (e.g. currentDate.getMonth())
    3. Invoke the "new Date" constructor with three parameters (currentYear, currentMonth, 1) to get the first day of the month
    4. Invoke the method to get the day of the week for the first day of the month (firstDay.getDay())

  • #3
    New to the CF scene
    Join Date
    Mar 2012
    Location
    Wisconsin
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I don't know how to do #3:

    Invoke the "new Date" constructor with three parameters (currentYear, currentMonth, 1) to get the first day of the month

    Where do I put this and how?

  • #4
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    911
    Thanks
    0
    Thanked 121 Times in 120 Posts

  • #5
    New to the CF scene
    Join Date
    Mar 2012
    Location
    Wisconsin
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    i looked at the w3schools and other sites for help as well as my book, but since this is such a specific scenario with how she wants all the dates to appear, unfortunately those do not help me.

  • #6
    New to the CF scene
    Join Date
    Mar 2012
    Location
    Wisconsin
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    guys, i have most of the code done, take a look at it! I just need to know where my errors lie (with listing the dates) and how to do the following:

    5. Determine the name of the first day of the current month.
    a. Using the correct Date Object method, get the integer for the day of the week for the first day of the current month. (hint: you will need to pass the year, month and the day value of 1 to the Date Object to get this number)

    6. Use a document.write statement to display every day of the current month on a new line in the format of the name of the day followed by the integer for the date.
    a. Use a while loop to keep track of the number of days written for the month.
    b. Use a for loop to write out the days of the week from the array created in step 2 under the name of the current month and year.

  • #7
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    The correct invokation of the Date constructor with year, month and day is
    Code:
    var firstDay = new Date(currentYear, currentMonth, 1); // 1 is the first day of the month of course :-)

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,216
    Thanks
    75
    Thanked 4,344 Times in 4,310 Posts
    In other words:
    Code:
    var now = new Date()
    var firstOfCurrentMonth = new Date( now.getFullYear(), now.getMonth(), 1 );
    Or a simpler way:
    Code:
    var now = new Date();
    var firstOfCurrentMonth = now.setDate(1);
    [But that simpler version doesn't follow what the instructions asked for. Meaning that, as usual, homework problems are not necessarily "real world."]
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,634
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Old Pedant View Post
    [But that simpler version doesn't follow what the instructions asked for. Meaning that, as usual, homework problems are not necessarily "real world."]
    There's a lot of things in this question that don't reflect the real world - such as using the completely obsolete document.write statement.

    Presumably this is a history course on how JavaScript used to be written back in the 20th Century (the same way the w3schools site provides a hoistorical JavaScript reference from the 20th Century rather than one for how JavaScript should be written now).

    This would be a reason why it is almost impossible to provide homework help in JavaScript without having both a copy of the actual question and near complete code for the solution - the question is asking for a 20th Century style script to be written and there are dozens of better ways to write the code now compared to what has been asked for. Without the question and the almost working code any "help" offered would probably use far more modern and efficient processing than has yet been taught in the course (assuming that the course even attempts to teach any real world modern JavaScript) and so would be very obviously NOT written by the person taking the history class.
    Last edited by felgall; 03-26-2012 at 09:31 PM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,216
    Thanks
    75
    Thanked 4,344 Times in 4,310 Posts
    Quote Originally Posted by felgall View Post
    T
    Presumably this is a history course on how JavaScript used to be written back in the 20th Century.
    LMAO! I love it!

    Too much truth!

    Of course, that means he has already failed the course. Look at the first line:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #11
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,634
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Old Pedant View Post
    LMAO! I love it!

    Too much truth!

    Of course, that means he has already failed the course. Look at the first line:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    document.write iscompletely invalid when you use XHTML and always has been. It was only ever valid with HTML. The script would fail to work at all if you use document.write in a page properly served as XHTML.

    JavaScript for XHTML is significantly different in what it allows and what you must use compared to JavaScript for HTML.

    So with that doctype the script requested will never work (and in IE8 and earlier the page will be offered for download rather than displaying at all).
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •