To Lecture Notes

IT 212 -- Jan 15, 2025

Review Exercises

  1. List the JavaScript operators that you remember from IT 130.
    Arithmetic Operators: + - *  ** / %  ++  --
    Comparison Operators: ==  ===  !=  !===  <  <=  >  >=
    Logical Operators: &&  ||
    Assignment Operators: =  +=  -=  *=  /=  %=
    Special Operator: typeof
  2. 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 = "pomegranate";
    document.writeln(b + " " + typeof b + "<br>");
    // Output: pomegranate string
    
    var c = false;
    document.writeln(c + " " + typeof c + "<br>");
    // Output: false boolean 
    
    var d = 1384 / 0;
    document.writeln(d + " " + typeof d + "<br>");
    // Output: Infinity number
    
    var e = "w" / 25;
    document.writeln(e + " " + typeof e + "<br>");
    // Output: NaN number
    
    var f;
    document.writeln(f + " " + typeof f + "<br>");
    // Output: undefined undefined
    
    var x = 1.43e678;
    document.writeln(x + " " + typeof x + "<br>");
    // Output: Infinity number
    
    var g = "123", h = 123;
    document.writeln((g == h) + " " + (g === h) + "<br>");
    // Output: true false
    
    document.writeln("abc");
    document.writelm("def");
    document.writeln("ghi");
    // Output: abc
    // Note that document.writelm is not a method, so the
    // JavaScript script stops here and displays an error
    // message in the debugging console.
    

More about Datatypes

Practice Quiz

Some Math Methods

Practice Problems

  1. Work Practice Problem 2 from the Jan 13 Notes. Answer:
    <!DOCTYPE html>
    <!-- Method 1. Use onclick attribute in button.
         All source code is in one file. -->
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Test Button</title>
        <script>
        function changeValue( ) {
            var button1 = document.getElementById("btn1");
            button1.value = "I've been clicked.";
        }
        </script>
    </head>
    <body>
        <h1>Test Button</h1>
    
        <input id = "btn1" type="button" 
               value="Click Me" onclick="changeValue( );">
    </body>
    </html>
    ======================================================
    <!DOCTYPE html>
    <!-- Method 2. Use addEventListener method to attach
                   event listener to button. Put HTML, CSS, 
                   and JS code in separate files. 
                   Source code file: index.html      -->
    <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>Test Button</title>
            <link rel="stylesheet" href="styles.css">
            <script src="script.js"></script>
        </head>
        <body>
            <h1>Test Button</h1>
            <input id = "btn1" type="button" value="Click Me">
        </body>
    </html>
    ----------------------------------------------------------
    /* Source code file: styles.css */
    body { font-family: Verdana;
           background-color: #F0F0FF;
           color: #000040; }
    ----------------------------------------------------------
    // Source code file: script.js
    function changeValue( ) {
        var button1 = document.getElementById("btn1");
        button1.value = "I've been clicked.";
    }
    
    function init( ) {
        var button1 = document.getElementById("btn1");
        button1.addEventListener("click", changeValue);
    }
    
    window.addEventListener("load", init);
  2. Work Practice Problem 3 from the Jan 13 Notes.
    Answer: see Practice Problem 3 in Jan 13 Notes.
  3. Design a web page that computes the monthly payment for a loan, given the principal, monthly payment, and term in years.
  4. 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.
    

Projects 1a and 1b

Control Structures

Arrays