Chapter 14 Review Questions
NOTE: The review questions for Chapter 14 were inadvertently left out of the mss, so they're included here.
14.6 Write the JavaScript code that creates three arrays, each with 100 elements, and named countingNumbers, evenNumbers, and oddNumbers. Then use a for loop to fill the countingNumbers array with the numbers 1 to 100, another for loop to fill the evenNumbers array with the numbers 2 to 200, and a third for loop to fill the oddNumbers array with the numbers 1 to 199.
Answer:
var countingNumbers = new Array(100)
var evenNumbers = new Array(100)
var oddNumbers = new Array(100)
var ctr
for (ctr=0; ctr<=99; ctr++) { //May also use ctr<100 instead of ctr<=99
countingNumbers[ctr] = ctr + 1 //Could also have the loop go from 1 to 100 and then
} // write countingNumbers[ctr-1] = ctr
for (ctr=0; ctr<=99; ctr++) { //May also use ctr<100 instead of ctr<=99
evenNumbers[ctr] = 2*(ctr+1) //Could also have the loop go from 1 to 100 and then
} // write evenNumbers[ctr-1] = 2*ctr
for (ctr=0; ctr<=99; ctr++) { //May also use ctr<101 instead of ctr<=100
oddNumbers[ctr] = 2*(ctr+1) – 1 //Could also have the loop go from 1 to 100 and then
} // write oddNumbers[ctr-1] = 2*ctr - 1
14.7 Write the JavaScript code that uses a prompt box to ask the surfer to enter a number from 2 to 10. Then create an array with that number of elements (i.e., whatever number the surfer entered) and use a for loop to fill the array completely with random numbers between 0 and 1. (Use the random number function covered in Chapter 11.)
Answer:
var theNumber = prompt("Please enter a number from 2 to 10", " ")
var randomNumbers = new Array(theNumber)
var ctr
for (ctr=0; ctr<theNumber; ctr++) { //May also use ctr<=(theNumber-1) instead of
randomNumbers[ctr] = Math.random() // ctr<theNumber
}
14.10 Write the code for a function named maxValue() with two parameters, named theArray and numValues. The function should use a loop and a conditional statement to find the maximum value in the array and then return that value.
Answer:
The basic algorithm to find the max value in the array is as follows:
1. Declare a variable named theMax, which will store the maximum value.
2. Set the value of theMax to the first value in the array. In other words, we start off by assuming that the first value in the array is the maximum value.
3. Proceed through the array one element at a time using a for loop. For each element, use a conditional statement to check if the value of the element is greater than the value stored in theMax. If it is greater, then we make that value the new value of theMax and continue to the next element. If it isn’t, we do nothing and continue to the next element.
4. Once we have gone through the whole array, the true maximum value will be stored in theMax.
Code:
function maxValue(theArray, numValues) {
var theMax = theArray[0]
for (ctr=1; ctr<numValues; ctr++) {
if (theArray[ctr] > theMax) {
theMax = theArray[ctr] //Change the value of theMax to the new greater value
}
}
return theMax
}
14.11 Rewrite the question1 and question2 conditional statements at the beginning of Section 14.5.2 so that they use the forms array. Then rewrite them again using a for loop so that the code only has one compound conditional statement (inside the loop). Show the change you would make to the loop if there were twenty-five questions instead of just two.
(NOTE: This question was slightly revised from the original mss version—the word “compound” was added before “conditional statement”.)
Answer:
The key change is to replace the form name question1 with forms[0] and the form name question2 with forms[1]:
var scoreA = 0, scoreB = 0, scoreC = 0
if (document.forms[0].choiceRB[0].checked) {
scoreA = scoreA + 1
}
else if (document.forms[0].choiceRB[1].checked) {
scoreB = scoreB + 1
}
else if (document.forms[0].choiceRB[2].checked) {
scoreC = scoreC + 1
}
if (document.forms[1].choiceRB[0].checked) {
scoreA = scoreA + 1
}
else if (document.forms[1].choiceRB[1].checked) {
scoreB = scoreB + 1
}
else if (document.forms[1].choiceRB[2].checked) {
scoreC = scoreC + 1
}
Rewrite as a for loop:
var ctr
for (ctr=0; ctr<2; ctr++) { //May also use ctr<=1 instead of ctr<2
if (document.forms[ctr].choiceRB[0].checked) {
scoreA = scoreA + 1
}
else if (document.forms[ctr].choiceRB[1].checked) {
scoreB = scoreB + 1
}
else if (document.forms[ctr].choiceRB[2].checked) {
scoreC = scoreC + 1
}
}
Modifying the for loop for 25 questions:
for (ctr=0; ctr<25; ctr++) { //May also use ctr<=24 instead of ctr<25
//Same conditional statement code as above
}
Chapter 15 Review Questions
15.3
var firstLetter1 = customer1.charAt(0) //customer1.substring(0,1) could also be used
var firstLetter2 = customer2.charAt(0)
if (firstLetter1 < firstLetter2) {
document.resultsForm.sortBox.value = customer1 + ", " + customer2
}
else if (firstLetter1 > firstLetter2) {
document.resultsForm.sortBox.value = customer2 + ", " + customer1
}
else {
document.resultsForm.sortBox.value = "Both customer names start with the same letter."
}
15.5
The code for the form will be:
<form id="londonForm" name="londonForm">
<input type="button" name="b1" value="Display London Time" onclick="londonTime()" /><br />
Day of week (0 = Sunday): <input type="text" name="dayBox" size="1" /><br />
Hours: <input type="text" name="hoursBox" size="2" /><br />
Minutes: <input type="text" name="minutesBox" size="2" /><br />
</form>
Where the function is:
function londonTime() {
var currentDate = new Date() //Create date object
document.londonForm.dayBox.value = currentDate.getUTCDay()
document.londonForm.hoursBox.value = currentDate.getUTCHours()
document.londonForm.minutesBox.value = currentDate.getUTCMinutes()
}
15.9
function setDateCookie() {
var currentDate = new Date()
var currentMonth = currentDate.getMonth()
var currentDayOfMonth = currentDate.getDate()
var expDate = currentDate.setMonth(currentMonth + 9)
document.cookie = "lastVisitMonth=" + currentMonth + ";expires=" + expDate.toGMTString()
document.cookie = "lastVisitDate=" + currentDayOfMonth + ";expires=" + expDate.toGMTString()
}