To Lecture Notes

IT 212 -- Jan 22, 2025

Review Exercises

  1. 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.
  2. 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.
  3. 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
    
  4. 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>");
        }
    }
    
  5. 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>");
    }
    
  6. 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>");
    }
    
  7. 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>
    

Practice Quiz

Expression Interpolation

Comparisons for Equality