# COMP 110 ```110-11a.1
COMP 110
Write JavaScript code to do the following.
Answers. Code is enclosed in &lt;script type=&quot;text/javascript&quot;&gt;…&lt;/script&gt;
var n = prompt(&quot;Please enter a number.&quot;);
3. Read two numbers and display their product.
var n1 = prompt(&quot;Please enter a number.&quot;);
var n2 = prompt(&quot;Please enter another number.&quot;);
alert(&quot;The product of &quot;+n1+&quot; and &quot;+n2+&quot; is &quot;+ n1*n2);
4. Read two numbers and display their sum. What problem did you encounter?
var n1 = 1*prompt(&quot;Please enter a number.&quot;);
var n2 = 1*prompt(&quot;Please enter another number.&quot;);
alert(&quot;The sum of &quot;+n1+&quot; and &quot;+n2+&quot; is &quot;+ n1+n2);
If you leave out multiplying the numbers by 1, you will get concatenation
5. Read in two numbers and display the larger.
var n1 = 1*prompt(&quot;Please enter a number.&quot;);
var n2 = 1*prompt(&quot;Please enter another number.&quot;);
if (n1 &gt; n2)
else
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 &quot;99&quot; is greater than the string &quot;100&quot;.
110-11a.2
6. Read in two numbers and display then in ascending order.
var n1 = 1*prompt(&quot;Please enter a number.&quot;);
var n2 = 1*prompt(&quot;Please enter another number.&quot;);
if (n1 &gt; n2)
else
7. Use a loop to display the numbers 0 through 5, each in a separate alert window.
for (var i=0; i&lt;=5; i++)
{
}
8. Use a loop to display the numbers 0 through 5 in a single alert window.
var s=&quot;&quot;; // Create empty string.
for (var i=0; i&lt;=5; i++)
{
s = s + i + &quot; &quot;; // Add next integer plus a space.
}
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&lt;=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&lt;=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(&quot;Please enter a number in the range 0…100&quot;);
while (n&lt;0 || n&gt;100)
{
alert(n +&quot; is out of range. Try again.&quot;);
n = prompt(&quot;Please enter a number in the range 0…100&quot;);
}
// Solution 2: Put the test inside the loop.
while (true)
{
var n = prompt(&quot;Please enter a number in the range 0…100&quot;);
if (n&gt;=0 &amp;&amp; n&lt;=100) break;
alert(n +&quot; is out of range. Try again.&quot;);
}
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(&quot;Please enter a number in the range 0…100&quot;);
while (isNaN(n) || n&lt;0 || n&gt;100)
{
alert(n +&quot; is out of range or not a number. Try again.&quot;);
n = prompt(&quot;Please enter a number in the range 0…100&quot;);
}
// Solution 2: put the test inside the loop.
while (true)
{
var n = prompt(&quot;Please enter a number in the range 0…100&quot;);
if (!isNaN(n) &amp;&amp; n&gt;=0 &amp;&amp; n&lt;=100) break;
alert(n +&quot; is out of range or not a number. Try again.&quot;);
}
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 (&quot;Please enter an integer.&quot;);
var sum = 0;
for (var i=0; i&lt;=max; i++)
sum = sum + i; // Add i to the running sum.
alert(&quot;The sum of 0 through &quot;+max+&quot; is &quot;+sum);
// Solution 2: Use Gauss' formula
// Get the upper bound.
var max = 1*prompt (&quot;Please enter an integer.&quot;);
alert(&quot;The sum of 0 through &quot;+max+&quot; is &quot;+(max*max+max)/2);
```