To Lecture Notes

IT 238 -- Apr 21, 2025

Review Exercises

  1. Test the array lookup operator [ ]. Answer:
    var a = ["abc", "def", "ghi", "jkl", "mno"];
    document.writeln(a[3]);
    // Output:
    jkl
    
    Recall that array indices in JavaScript are zero-based.
  2. Explain what properties and methods are for an object.
    Answer: a property is a piece of data that is stored in an object; a method is a procedure that can be called from the object to do something.
  3. Test these Array properties and methods:
    length  pop  push  shift  unshift  
    Answer:
    var a = [3, 6, 2, 7, 4];
    a.pop( );      --> 3,6,2,7
    a.push(88);    --> 3,6,2,7,88
    a.shift( );    --> 6,2,7,88
    a.unshift(99); --> 99,6,2,7,88
    document.writeln(a.toString( ));
    // Output:
    99,6,2,7,88
    
  4. Test these String properties and methods:
    charAt  length  indexOf  toLowerCase 
    Answer:
    var a = "elephant";
    document.write(a.charAt(4) + " ");
    // Output: h
    document.write(a.length + " ");
    // Output: 8
    document.write(a.indexOf("an") + " ");
    // Output: 5
    document.writeln(a.toLowerCase( ) + "<br>")
    // Output: ELEPHANT
    

Practice Quiz 4a

Object Literals

JavaScript Object Notation (JSON)

Practice Problems

  1. Convert the JSON string defined in the Kids Data file kidsdata.js file into an array of object literals. Then print the names of the kids in the array. Answer:
    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>Document</title>
            <script src="kids.js"></script>
            <script>
            var objects = JSON.parse(kids);
            for(var kid of objects) {
                document.writeln(kid.name + "<br>");
            }
            </script>
        </head>
        <body></body>
    </html>
    
  2. Print the names and ages of the girls in the Kids Data file kidsdata.js file. Answer:
    // Script only:
    var objects = JSON.parse(kids);
    for(var kid of objects) {
        if (kid.gender == "F") {
            document.writeln(`${kid.name} ${kid.age}<br>`);
        }
    }
    
  3. Print the gender and age of Jane. Answer: see KidsTextArea1 Example below.
  4. Print the gender and age of the kid whose name is entered in a prompt box.  Answer: see below.
  5. Display the gender and age of the kid when the name is entered in a textfield. Answer: see below.
  6. Print the names and ages of all the kids when the gender is entered in a textfield. Display the output in a textarea. Answer: see below.
  7. Look at this KidsTextArea1 Example
    This example displays output from questions 1, 2, and 3 in a textarea controls:
    Web Page   Source Code
  8. Look at this KidsTextArea2 Example
    This example displays output from questions 4 and 5 in a paragraph and in a text area control, respectively:
    Web Page   Source Code

Project 2a