Java Reference
In-Depth Information
}
var timesTable;
while ((timesTable = prompt("Enter the times table", -1)) != -1) {
while (isNaN(timesTable) == true) {
timesTable = prompt(timesTable + " is not a " +
"valid number, please retry", -1);
}
if (timesTable == -1) {
break;
}
document.write("<br />The " + timesTable +
" times table<br />");
writeTimesTable(timesTable, 1, 12);
}
</script>
</body>
</html>
Save this as ch4 _ question2.html .
The function remains the same, so let's look at the new code. The first change from Question 1 is
that you declare a variable, timesTable , and then initialize it in the condition of the first while
loop. This may seem like a strange thing to do at first, but it does work. The code in parentheses
inside the while loop's condition:
(timesTable = prompt("Enter the times table",-1))
is executed first because its order of precedence has been raised by the parentheses. This will return
a value, and it is this value that is compared to ‐1 . If it's not ‐1 , then the while condition is true ,
and the body of the loop executes. Otherwise it's skipped over, and nothing else happens in this
page.
In a second while loop nested inside the first, you check to see that the value the user has entered is
actually a number using the function isNaN() . If it's not, you prompt the user to try again, and this
will continue until a valid number is entered.
If the user had entered an invalid value initially, then in the second while loop, that user may have
entered ‐1 , so following the while is an if statement that checks to see if ‐1 has been entered. If it
has, you break out of the while loop; otherwise the writeTimesTable() function is called.
Chapter 5
exercise 1 Question
Using the Date type, calculate the date 12 months from now and write this into a
web page.
Search WWH ::




Custom Search