- Look at the source code for the MonthlyPayment Example
Web Page
Source Code
What is the ** operator?
Answer: ** is the exponentiation operator;
5 ** 3 means 5 raised to the 3rd power =
5 * 5 * 5 = 125.
- What were the control structures mentioned in Edsgar Dykstra's seminal letter
Go To Considered Harmful sent to the
editor of the journal of the Association of Computing Machinery (ACM) in 1968?
Look at how traditional for loops work.
Answer: The two control structures are if B then A (decision) and
while B repeat A (repetition).
These are in addition to sequence, which means listing statements in order.
- Look at the
Control Structures Document.
Explain how the traditional for loop works.
Answer: The traditional for loop header has three parts: initialization, condition,
and iteration. Here are three examples:
// Display the numbers from 1 to 20:
for(var n = 1; n <= 20; i++) {
document.writeln(n + " ");
}
// Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Display the odd numbers less than 20:
for(var n = 1; n < 20; i += 2) {
document.writeln(n + " ");
}
// Output: 1 3 5 7 9 11 13 15 17 19
// Start with 1 and keep doubling
// the number while the number is
// less than 100.
for(var n = 1; n < 1000; i *= 2) {
document.writeln(n + " ");
}
// Output: 1 2 4 8 16 32 64
- Write a for loop that prints the integers 1 to 1,000 on a browser page.
Put 20 numbers on each line. Hint: whenever n % 20 == 0
for a number, write
"<br>"
Answer:
for(var n = 1; n <= 1000; n++) {
document.writeln(n + " ");
if (n % 10 == 0) {
document.writeln("<br>");
}
}
- Using the document.write method, write a double
for loop that writes the following to
the browser document:
*
**
***
****
*****
******
*******
Use a prompt box to enter the number of rows. Answer:
var numRows = parseInt(prompt("Enter number of rows", "0"));
for(var i = 1; i <= numRows; i++) {
for(var j = 1; j <= i; j++) {
document.write("*");
}
document.writeln("<br>");
}
- This problem was given as part of a job interview programming test, according to
the
Eloquent JavaScript textbook. Write a loop that
prints the numbers from 1 to 100. If a number is divisible by 3,
print Fizz after it. If the number is divisible by 5,
print Buzz after it. If the number is divisible by both 3 and 5,
print Fizz Buzz after it. Here is the beginning of the output:
1
2
3 Fizz
4
5 Buzz
6 Fizz
7
8
9 Fizz
10 Buzz
11
12 Fizz
13
15 Fizz Buzz
16
17
...
Answer:
for(var i = 1; i <= 100; i++) {
document.write(i);
if (i % 3 == 0) {
document.write(" Fizz");
}
if (i % 5 == 0) {
document.write(" Buzz");
}
document.writeln("<br>");
}
- Write a function convert that inputs a length in meters then outputs the length feet and inches.
Test your function like this:
document.writeln(convert(1.0));
// Output: 3'3"
Answer:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script>
function convert(m) {
// There are 2.54 cm per inch.
var ftFloat = m * 100 / (2.54 * 12);
var ftInt = Math.floor(ftFloat);
var inInt = Math.round((ftFloat - ftInt) * 12);
var output = ftInt + "\' " + inInt + "\"";
// The previous line can be rewritten as
// var output = `${ftInt}\' ${inInt}\"`;
return output;
}
var mString = prompt("Enter length in meters.");
var mFloat = parseFloat(mString);
alert("Length in FtIn: " + convert(mFloat));
</script>
</head>
<body></body>
</html>
- You are familiar with the comparison operator ==,
used in JavaScript, Python, Java, and C#.
- This operator acts somewhat differently in each language.
- The JavaScript == operator is liberal in its comparisons.
- x == y returns true if x equals
y after conversion to the same datatype.
- For example,
5 == 5, "5" == "5", 5 == "5", 0 == false, 1 == true
all return true.
- The === operator is strict in its comparisons.
- x === y returns true if the values
x and
y are equal and also are of the same datatype.
- For example 5 === "5", 0 === false, and
1 === true all return false
because, in each case, the operands are not of the same datatype.
- Predict the output of these statements:
document.writeln(`${5 == 5} ${5 == 3} ${"5" == "5"}<br>`);
// The string "5" is converted to a number before
// performing the comparison:
document.writeln(`${5 == "5"}`);
// Output:
true false true
true
- When performing a comparison, to convert a number to boolean (true or
false), the rule is that
zero converts to false, one converts to
true; any other value does not convert to
boolean.
Predict the output of this statement:
document.writeln(`${0 == false} ${5 == true} ${0.00000001 == false}`);
// Output:
true false false
- When performing a comparison, to convert a string to boolean,
the rule is that the strings
"", "0", and
"0.0"
convert to false, "1" and
"1.0"
convert to true. Any other string does not convert to
boolean.
Predict the output of these statements:
document.writeln(`${"1" == true} ${"" == false}<br>`);
document.writeln(`${"0" == false} ${"1" == true} ${"5" == true}<br>`);
document.writeln(`${"0.00" == false} ${"1.00" == true} ${"5.00" == true}`);
// Output:
true true false
true true false
true true false
- The === comparison operator only returns
true if the datatypes of the operands are the same
without converting.
Predict the output of these statements:
document.writeln(`${5 === "5"}<br>`);
document.writeln(`${"" === false} ${"1" === true}<br>`);
document.writeln(`${"0" === false} ${"1" === true}<br>`);
document.writeln(`${"0.00" === false} ${"1.00" === true}`);
// Output
false
false false
false false
false false
- Practice Problem: Predict the output:
var n = 1.0 - 0.8;
var m = 0.3 - 0.1;
document.writeln((n == m) + " ");
// Output:
false
Why? Because of differing roundoff errors
due to the binary representation of the double precision floating point numbers,
1.0 - 0.8 and
0.3 - 0.1 are not represented with exactly
the same values. To see this, look at the output of this statement:
document.writeln((n - m));
// Output:
-2.7755575615628914e-17
Instead of checking for equality with floating point values, we should check
that n and
m are very
close to each other:
document.writeln((Math.abs(n - m) < 1.0e15) + " ");
// Output:
true