To Lecture Notes

IT 238 -- Apr 9, 2025

Review Exercises

  1. List the JavaScript operators that you remember from IT 130.
    Arithmetic Operators: + - *  ** / %  ++  --
    Comparison Operators: ==  ===  !=  !===  <  <=  >  >=
    Logical Operators: &&  ||
    Assignment Operators: =  +=  -=  *=  /=  %=
    Increment and Decrement Operators: ++ --
    Special Operator: typeof
  2. What are the JavaScript datatypes that listed at the end of class on Monday?
    Answer: number string boolean undefined
  3. For this exercise, the typeof operator returns a string that represents the datatype of the expression after it. What is displayed in the browser by each of these JavaScript code fragments?
    var a = 45.3;
    document.writeln(a + " " + typeof a + "<br>");
    // Output: 45.3 number
    
    var b = -163;
    document.writeln(b + " " + typeof b + "<br>");
    // Output: -163 number
    
    var c = "pomegranate";
    document.writeln(c + " " + typeof c + "<br>");
    // Output: pomegranate string
    
    var d = false;
    document.writeln(d + " " + typeof d + "<br>");
    // Output: false boolean
    
    var e = 1384 / 0;
    document.writeln(e + " " + typeof e + "<br>");
    // Output: Infinity number
    
    var f = "w" / 25;
    document.writeln(f + " " + typeof f + "<br>");
    // Output: NaN number
    
    var f2 = 0 / 0;
    document.writeln(f2 + " " + typeof f2 + "<br>");
    // Output: NaN number
    
    var g;
    document.writeln(g + " " + typeof g + "<br>");
    // Output: undefined undefined
    
    var h = 1.43e678;
    document.writeln(h + " " + typeof h + "<br>");
    // Output: Infinity number
    
    document.writeln("abc");
    document.writelm("def");
    document.writeln("ghi");
    // Output: abc
    // In Line 2, writeln is mispelled as writelm.
    // Open the Chrome Console to see the error message. 
    
  4. What is the output?
    var i = 1, n = 2;
    while(i <= 11) {
        document.write(n + " ");
        i++;
        n = n * n;
    }
    document.writeln("<br><br>");
    document.writeln(Number.MAX_VALUE);
    // Output:
    2 4 16 256 65536 4294967296 18446744073709552000 3.402823669209385e+38 
         1.157920892373162e+77 1.3407807929942597e+154 Infinity
    
    1.7976931348623157e+308
    

More about Datatypes

Functions

Project 1a

Practice Quiz

Some Math Methods

Practice Problems

  1. Write HTML, CSS, and JavaScript code in separate files that displays a button on the page that looks like this:



    When the button is clicked, the caption changes to "I've been clicked." Answer:
    ==========================================
    ------------------------------------------
    <!DOCTYPE html>
    <!-- ButtonTest Example
    Source code file: index.html-->
    <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>ButtonTest Example</title>
            <link rel="stylesheet" href="styles.css">
            <script src="script.js"></script>
        </head>
        <body>
            <h1>Button Test Example</h1>
            <button id="btn1">Click Me</button>
        </body>
    </html>
    ------------------------------------------
    /* ButtonTest Example 
       Source code file: styles.css */
    
    body { font-family: Verdana, sans-serif; }
    button { width: 150px; }
    ------------------------------------------
    / ButtonTest Example
    // Source code file: script.js
    
    // Event listener
    function changeCaption( ) {
        var button1 = document.getElementById("btn1");
        button1.innerHTML = "I\'ve been clicked.";
    }
    
    // init method adds event listener to button.
    function init( ) {
        var button1 = document.getElementById("btn1");
        button1.addEventListener("click", changeCaption);
    }
    
    // init method executes after page is loaded.
    window.addEventListener("load", init);
    ==========================================
    
  2. Design a web page that computes the monthly payment for a loan, given the principal, monthly payment, and term in years.
  3. Here is a script (similar to your submission for Project 1a) to test the computeMP function:
    // Test the computeMP function, which computes the monthly
    // payment for a loan.  The function parameters are
    // p=principal, r=interest rate, n=term of loan in years.
    function computeMP(p, r, n) {
        var mp = (p * r / 1200.0) /
            (1 - (1.0 + r / 1200.0) ** (-12.0 * n));
        return mp;
    }
    var principal = 100000;
    var rate = 6;
    var term = 15;
    var mp = computeMP(principal, rate, term);
    mp = Math.round(mp * 100) / 100;
    document.writeln(mp);
    // Output should be 843.86.
    

Project 1b