To Lecture Notes

IT 231 -- Feb 27, 2024

Review Exercises

  1. Consider this compound object literal:
    persons = {
        ID1084: { firstName: 'Alice', lastName: 'Liddel' },
        ID1382: { firstName: 'Wayne', lastName: 'Campbell' }
    };
    1. Print the entire persons object.
    2. Print the person with key ID1084.
    3. Print the last name of the person with key ID1382.
    4. Send the first and last name with key=ID1382 to the index.ejs view as the local variables first and last. Put them in a table that looks like this:
      First Name Last Name
      Alice Liddel
      Wayne Campbell
    Answer:
    -------------------------------------------
    // Source code file: main.js
    var persons = {
        ID1084: { firstName: 'Alice', lastName: 'Liddel' },
        ID1382: { firstName: 'Wayne', lastName: 'Campbell' }
    };
    
    // Write this information to the Node console.
    console.log(persons);
    console.log(persons.ID1084);
    console.log(persons.ID1382.lastName);
    
    // Display the first and last names of the
    // person with ID=ID1382 in a table.
    const express = require("express");
    const app = express( );
    
    app.set("view engine", "ejs");
    
    app.get("/person_names", (req, res) => {
        res.render("person_names", 
            { first: persons.ID1382.firstName, 
              last: persons.ID1382.lastName });
    });
    app.listen(3000, ( ) => {
        console.log("Server started");
    });
    
    -------------------------------------------
    <!DOCTYPE html>
    <!-- Source code file: view/person_names.ejs -->
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Person Names </title>
    </head>
    <body>
        <h1>Person Names</h1>
        <table>
            <tr> 
                <th>First Name</th>
                <th>Last Name</th>
            </tr>
            <tr>
                <td><%= first %></td>
                <td><%= last %></td>
            </tr>
        </table>
    </body>
    </html>
    
  2. Look at the solutions to exercises 5 and 6 from the Feb 22 notes.
  3. Look at these examples:
         TestEjs2   TestEjs3   TestEjs4   TestEjs5

Practice Quiz 7a