Why have I not posted for 6 months? New job in the IT department at the British Petroleum refinery in Blaine, WA and my senior year of school online at WGU for my Bachelor’s in IT Security – which includes gems like this:

The elements on a form need to be initialized with dates for the next week, starting from today. Only the <input> tags of type text need to be initialized. Consider the following XHTML code:

<head>
<script type=”text/javascript”>
function FillDate() {
var d = new Date();
// insert loop here
}
</script>
</head>
<body>
<form name=”calendarForm”>
<input type=”text” name=”date1″></input><br/>
<input type=”text” name=”date2″></input><br/>
<input type=”text” name=”date3″></input><br/>
<input type=”text” name=”date4″></input><br/>
<input type=”text” name=”date5″></input><br/>
<input type=”text” name=”date6″></input><br/>
<input type=”text” name=”date7″></input><br/>
<input type=”button” name=”fillDates” onclick=”FillDate()” value=”Fill Days”></input>
</form>
</body>

Given the code, which loop will accomplish this?
and this answer they want is this:

for (var i = 0; i < document.calendarForm.length; i++) {
if (document.calendarForm.elements.type == “text”) {
d.setDate(d.getDate() + 1);
document.calendarForm.elements.value = d.toDateString();
}
}

Line by line, what is this for loop doing? it looks to me like it’s checking to see if it’s a text box, and if it is it’s going to set the new date to whatever date it gets + 1 to what? the day? and I’m lost after that.

 I had the same problem. The answer says:

This answer uses the correct way to get the number of elements in the form via the length property.   How many elements?  7. That means 7 loops

To determine the element type, it uses the type attribute.  It is looking for the tag type=”text”

The next date is correctly calculated by adding one to the day number obtained with getDate and then setDate is used to get the date element.

 

This is where it gets tricky   – d.setDate(d.getDate() + 1); —– remember it does the thing inside the parentheses first. 

getDate gets the actual number date 1-31 from the computer the script is running on, adds one (1) to it, and puts that new number in variable “d”

setDate runs next and sets the day of the month in the Date object to our new number in “d”.

 Finally, the new date is assigned to the value property — one point that is NOT in the CIW text -“toDateString”

document.calendarForm.elements[i].value = d.toDateString();

In each time around the loop the new value of  the “d” variable is turned into a string with the format “Day mon date(this is a number 1-31) year”. This string is now the system date of the computer running the script PLUS 1 for each time through the loop. Literally it returns “day mon “d” year”

BONUS POINTS – THE DATE WILL ALWAYS START WITH “TOMORROW” BECAUSE d.setDate(d.getDate() + 1); ADDS ONE TO THE WHOLE MESS THE FIRST TIME THROUGH AND THE DATE BECOMES TODAY + ONE.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s