I have been truly struggling with Exercise 4. I am what you might call an intermediate beginner and if this question has come up before, I apologize, but I searched the forum and could not find a reference to this.
Exercise 4 calls for an automated times table that asks the user for what times table he or she wishes, then calculates and displays it. The process continues until the user enters -1, which breaks out of a while loop.
Being a little stumped, I checked the answer in Appendix A and entered this code. No matter what I did, it did not work properly, so I attempted modification of this code.
What happens no matter what I do is that if a user puts in a value other than -1, the same prompt for a number reappears until -1 is entered. Then the times table is calculated and displayed. In other words, there is an unwanted repetition of the first prompt box. Here is my modified code:
Code:
// Function to output multiplication table.
function doMultiplicationTable(tableValue, startValue, endValue) {
for (; startValue <= endValue; startValue++) {
document.write(tableValue + " * " + startValue + " = " +
(tableValue * startValue) + "<br />");
} // end for
} // end doMultiplicationTable(tableValue, startValue, endValue)
/*
*
* Get user input for calculation of timestable. Enclose within a
* while loop whose condition is to continue unless the times table
* value is equal to -1.
*
*/
var timesTable;
while ( ( timesTable = prompt("What times table do you want calculated?", -1) ) != -1 ) {
while ( isNaN(timesTable) ) {
timesTable = prompt(timesTable + " is not a valid number. Please retry.", -1);
}
if (timesTable == -1) {
break;
}
document.write("<p>The " + timesTable + " times table:</p>");
doMultiplicationTable(timesTable, 1, 12);
}
Any assistance or insight is greatly appreciated.
Sincerely,
Robert Hieger