To Lecture Notes

IT 238 -- Jan 21, 2026

Review Exercises

  1. Find the errors in the source code files contained in this project:
         meters-to-ft-in.zip.
    Answer: here is a zipfile of the corrected project.
  2. What are the four primitive JavaScript datatypes that we saw last week?
    Answer: string, boolean, number, and undefined. We also saw that null, bigint, and symbol are JavaScript datatypes, but you only need to know the first four for now.
  3. Name two two non-primitive JavaScript datatypes?
    Answer: object and function
  4. What is the JavaScript ** operator?
    Answer: ** is the exponentiation operator; 5 ** 3 means 5 raised to the 3rd power = 5 * 5 * 5 = 125.
  5. How do the Math.floor, Math.ceil, and Math.round methods work? Look at the Some Math Methods section of the Jan 14 Notes to answer this question.
  6. How can you use Math.round to round a floating point number to three digits after the decimal point.
    Answer:
    var amount = 23.5425643;
    var amtRounded = Math.floor(amount * 1000) + 1;
    // Details of calculation
    // Original amount: 23.5425643;
    // Amount after multiplying by 1000: 23542.5643
    // Amount after taking the floor:    23542
    // Amount after dividing by 1000:    235.42
    
  7. Use the JavaScript Math.random method, which returns a floating point number in the interval [0, 1), to return a random number in the set {1, 2, 3, 4, 5, 6}.
    var randomValue = Math.floor(Math.random( ) * 6) + 1
    // JS Expression       Random Output Range
    // Math.random( )      [0, 1)
    // Math.random( ) * 6  [0, 6)
    // Math.floor(Math.random( ) * 6)     {0, 1, 2, 3, 4, 5}
    // Math.floor(Math.random( ) * 6) + 1 {1, 2, 3, 4, 5, 6}
    

Control Structures

Practice Problems

  1. 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?
    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.
  2. This Road Runner cartoon shows two versions of the while loop.

  3.  Explain how a while loop is related to a traditional for loop.
    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
    // Corresponding while loop, which still has the iteration,
    // condition, and iteration statements, but not as conveniently
    // displayed on the same line:
    var n = 1;
    while(n <= 20) {
        document.writeln(n + " ");
        n++;
    }
    
    // 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
    // Corresponding while loop:
    var n = 1;
    while(n <= 20) {
        document.writeln(n + " ");
        n += 2;
    }
    
    // Start with 1 and keep doubling
    // the number while the number is
    // less than 100.
    for(var n = 1; n < 100; i *= 2) {
        document.writeln(n + " ");
    }
    // Output: 1 2 4 8 16 32 64
    // Corresponding while loop:
    var n = 1;
    while(n <= 20) {
        document.writeln(n + " ");
        n *= 2;
    }
    
  4. 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>");
    }