110-11a.1 COMP 110 First self help exercises: answers. Write JavaScript code to do the following. Answers. Code is enclosed in <script type="text/javascript">…</script> 1. Alert "Hello world." alert("Hello world"); 2. Read a number (using prompt) and display it using alert. var n = prompt("Please enter a number."); alert("Your number is "+ n); 3. Read two numbers and display their product. var n1 = prompt("Please enter a number."); var n2 = prompt("Please enter another number."); alert("The product of "+n1+" and "+n2+" is "+ n1*n2); 4. Read two numbers and display their sum. What problem did you encounter? var n1 = 1*prompt("Please enter a number."); var n2 = 1*prompt("Please enter another number."); alert("The sum of "+n1+" and "+n2+" is "+ n1+n2); If you leave out multiplying the numbers by 1, you will get concatenation instead of addition since prompt returns a string. 5. Read in two numbers and display the larger. var n1 = 1*prompt("Please enter a number."); var n2 = 1*prompt("Please enter another number."); if (n1 > n2) alert(n1+" is the larger."); else alert(n2+" is the larger."); If you leave out the multiplication by 1, you will get strange answers since prompt returns a string. For example, while the number 100 is greater than the number 99, the string "99" is greater than the string "100". 110-11a.2 6. Read in two numbers and display then in ascending order. var n1 = 1*prompt("Please enter a number."); var n2 = 1*prompt("Please enter another number."); if (n1 > n2) alert(n2+" "+n1); else alert(n1+" "+n2); 7. Use a loop to display the numbers 0 through 5, each in a separate alert window. for (var i=0; i<=5; i++) { alert(i); } 8. Use a loop to display the numbers 0 through 5 in a single alert window. var s=""; // Create empty string. for (var i=0; i<=5; i++) { s = s + i + " "; // Add next integer plus a space. } alert(s); 9. Use a loop to display the numbers in the range 0…20 that are multiples of 3. // Solution 1: Generate all numbers and test each. for (var i=0; i<=20; i++) { if (i%3==0) alert(i); // Display those that are // divisible by 3. } // Solution 2: Generate only multiples of 3. for (var i=0; i<=20; i=i+3) { alert(i); // Only multiples of 3 are generated. } 110-11a.3 10. Prompt the user for a number in the range 0…100. If the number is out of range, display an error message and prompt again until a valid number is entered. Assume the user enters a number each time. // Solution 1: Put the test in the while statement. var n = prompt("Please enter a number in the range 0…100"); while (n<0 || n>100) { alert(n +" is out of range. Try again."); n = prompt("Please enter a number in the range 0…100"); } // Solution 2: Put the test inside the loop. while (true) { var n = prompt("Please enter a number in the range 0…100"); if (n>=0 && n<=100) break; alert(n +" is out of range. Try again."); } 11. Repeat number 10, but this time allow for the possibility that the user enters something that is not a number. Hint: the built in function isNaN(x) returns true if x is not a number. It returns false if x is a number. Sort of backwards, but that's what's available. // Solution 1: put the test in the while statement. var n = prompt("Please enter a number in the range 0…100"); while (isNaN(n) || n<0 || n>100) { alert(n +" is out of range or not a number. Try again."); n = prompt("Please enter a number in the range 0…100"); } // Solution 2: put the test inside the loop. while (true) { var n = prompt("Please enter a number in the range 0…100"); if (!isNaN(n) && n>=0 && n<=100) break; alert(n +" is out of range or not a number. Try again."); } 110-11a.4 12. Prompt for an integer, then display the sum of the integers from 0 through the number entered. For example, if you enter 10, then display 55, which is the sum of 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10. // Solution 1: iteration. // Get the upper bound. var max = 1*prompt ("Please enter an integer."); var sum = 0; for (var i=0; i<=max; i++) sum = sum + i; // Add i to the running sum. alert("The sum of 0 through "+max+" is "+sum); // Solution 2: Use Gauss' formula // Get the upper bound. var max = 1*prompt ("Please enter an integer."); alert("The sum of 0 through "+max+" is "+(max*max+max)/2);