To Lecture Notes

IT 238 -- Apr 28, 2025

Review Exercises

  1. Rewrite this traditional for loop as a modern for loop:
    var output = "";
    for(var i = 0; i < kidsArray.length; i++) {
        if (kidsArray[i].age < 12) {
            output += kidsArray[i].name + "\n";
        }
    }
    
    Answer:
    var output = "";
    for(var kid of kidsArray) {
        if (kid.age < 12) {
            output += kid.name + "\n";
        }
    }
    
    
  2. We saw earlier that the following lines round the value of n to two digits after the decimal point:
    var n = 34.83728;
    var nRounded = Math.round(n * 100) / 100;
    
    Verify that the following line also rounds n to two digits after the decimal point:
    var nRounded = n.toFixed(2);
    // Answer: 34.84
    
  3. We know how to attach a click event listener f to a button object btn1:
    btn1.addEventListener("click", f);
    
    Show how to use the onclick property to add the event listener f to the button object btn1. Answer:
    btn1.onclick = f;
    
    Do the same thing for a load event listener.
    Answer:
    window.addEventListener("load", init);
    can also be written as
    window.onload = init;
    

Functions as First Class Objects

Anonymous Functions

Arrow Notation for Functions

Math Class Methods

Use Strict