Suggested solutions to these questions can be found in Appendix A.
Using document.write(), write code that displays the results of the 12 times table. Its output should be the results of the calculations. 12 * 1 = 12 12 * 2 = 24 12 * 3 = 36 ... 12 * 11 = 132 12 * 12 = 144 |
||
A: |
<html> <body> <script language=JavaScript> var timesTable = 12; var timesBy; for (timesBy = 1; timesBy < 13; timesBy++) { document.write(timesTable + " * " + timesBy + " = " + timesBy * timesTable + "<br>"); } </script> </body> </html> Save this as ch03_q2.htm. We use a for loop to calculate from 1 * 12 up to 12 * 12. The results are written to the page with document.write(). What's important to note here is the effect of the order of precedence; the concatenation operator (the +) has a lower order of precedence than the multiplication operator, *. This means that the timesBy * timesTable is done before the concatenation, which is the result we want. If this were not the case, we'd have to put the calculation in parentheses to raise its order of precedence. |
Modify the code of Question 3 to request the times table to be displayed from the user; the code should continue to request and display times tables until the user enters –1. Additionally, do a check to make sure that the user is entering a valid number; if it's not valid, ask her to re-enter it. |
||
A: |
<html> <body> <script language=JavaScript> function writeTimesTable(timesTable, timesByStart, timesByEnd) { for (;timesByStart <= timesByEnd; timesByStart++) { document.write(timesTable + " * " + timesByStart + " = " + timesByStart * timesTable + "<br>"); } } 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 ch03_q4.htm. The function remains the same, so let's look at the new code. The first change from Question 3 is that we 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, we check to see that the value the user has entered is actually a number using the function isNaN(). If it's not, then we 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 she may have entered –1, so following the while is an if statement that checks to see if -1 has been entered. If it has, we break out of the while loop; otherwise the writeTimesTable() function is called. |