To Lecture Notes

IT 231 -- Feb 20, 2024

Review Exercises

  1. If the Express server app is defined as
    const app = express( );
    
    what do these methods do?
    (a) app.get  (b) app.set  (c) app.use
    
    Answer:
    (a) An app.get call is of the form
    app.get(route, callback);
    
    If the route input into the URL field of the browser matches the route parameter, the callback function is executed.
    (b) An app.set call is of the form
    app.set(key, value);
    
    For example,
    app.set("view engine", "ejs");
    
    adds a setting to the browser app with key "view engine" and value "ejs".
    (c) app.use executes a middleware function. (We will look at the details of middleware software later.) The example we have seen is this:
    app.use(express.static("public"));
    
    This method call sets the public folder as a static folder.
  2. Which Array methods do you know?
    Answer: We discussed these Array methods previously in class:
    reverse  toString  sort  forEach (hypermodern for loop)
    
    Here are three for loops that print the items in an array:
    // Array definition:
    var arr = [4, 2, 7, 6, 8];
    
    // Traditional for loop:
    for(let i = 0; i < arr; i++) {
        console.log(arr[i]);
    }
    
    // Modern for loop:
    for(let n of arr) {
        console.log(n);
    }
    
    // Hypermodern for loop:
    arr.forEach( n => {
        console.log(n);
    });
    
    We also discussed the Array length property.

    You need parentheses when accessing a method, even if there are no parameters:
    var i = a.indexOf("page");
    // or
    a.sort( );
    
    The difference in accessing an Array method vs. an Array property:
    Don't use parentheses when accessing a property, for example: a.length.-->
  3. Look up these Array methods in the W3Schools documentation:
    push  pop  shift  unshift  slice  splice
    
    Try out these methods using REPL. Answer: Here is our REPL session from class:
    > var a = [3, 6, 4, 5, 7];
    undefined
    > a.pop( )
    7
    > a
    [3, 6, 4, 5]
    
    > var a = [3, 6, 4, 5, 7];
    undefined
    > a.push(9);
    6
    > a
    [3, 6, 4, 5, 7, 9]
    
    > var a = [3, 6, 4, 5, 7];
    undefined
    > a.shift( )
    3
    > a
    [6, 4, 5, 7]
    
    > var a = [3, 6, 4, 5, 7];
    undefined
    > a.unshift(9);
    6
    > a
    [9, 3, 6, 4, 5, 7]
    > a.shift( );
    9
    > a
    [3, 6, 4, 5, 7]
    > a.splice(1, 3, 97, 98]
    [6, 4, 5]
    > a
    [3, 97, 98, 7]
    

Practice Quiz 6a

Embedded JavaScript Files

The Magic8Ball Example

  1. Create an Express project folder named magic-8ball.
  2. Create this main script: main.js
  3. Create an Express project with npm init.
  4. Install Express with npm i express.
  5. Install the Ejs package with npm i ejs.
  6. Create a views folder in your Express project folder.
  7. Place these EJS files in the views folder:
         magic-8ball.ejs   route-not-found.ejs
  8. Run your Express project using node main.js.
  9. View your app in a browser using this URL:
    localhost:3000/prediction