To Lecture Notes

IT 231 -- Dec 6, 2024

Review Exercises

  1. Which method do you use to convert an object literal into a JSON string?
    Answer: JSON.stringify
  2. Which method do you use to convert a JSON string into an object literal?
    Answer: JSON.parse
  3. Predict the output:
    var a = [34, 165, 11649, 4];
    console.log(a.sort( ));
    
    Answer: if we execute this code, the output is
    [11649, 165, 34, 4]
    
    To sort these numbers JavaScript first changes them into strings, then sorts them in dictionary order. To sort them as numbers, pass a function to a.sort that tells how to compare two numbers. This function should return a positive number when a > b , it should return a negative number when a < b , and it should return zero when a == b .  The function that does this is defined as (a, b) => a - b;  if a > b , a - b is positive, if a > b, a - b is negative, and if a == b , a - b is 0 . Here are the JavaScript statements:
    var a = [34, 165, 11649, 4];
    console.log(a.sort( (s, t) => {
        if (s > t) {
            return 1;
        }
        else if (s < t) {
            return -1;
        }
        else {
            return 0;
        }
    }));
    
  4. What is the Node term for a URL entered in the browser? For example:
    localhost:3000/home_page
    
    The URL is "/home_page". Answer: route
  5. How do you obtain the current URL in a main.js script? Answer:
    // req is the request object.
    var route = req.url;
    

Practice Quizzes

Midterm Review Guide

The File System Module fs

Reference: W3Schools Node.js File System Module
www.w3schools.com/nodejs/nodejs_filesystem.asp

Reading from Files

Tutorial 3

Project 1

Writing to Files