To Lecture Notes

IT 231 -- Jan 18, 2024

Review Exercises

  1. List the JavaScript datatypes that you know. Answer:
    string  number  boolean  undefined
    
  2. What is the difference between these JavaScript keywords?
    var  const
    
    Answer: var is used to create and initialize a variable whose value can be changed later:
    var n = 384;
    n = 391;
    
    const is used to create a constant variable that cannot be changed later:
    const PI = 3.14159265;
    
  3. What is a function?
    Answer: A function is a group of statements that can be tested and called by other JavaScript scripts. Functions are important for modularity. A function can accept parameters, which are input values passed in to the function. This function can also return a value.
  4. Write a function named makeGreeting that inputs a name of a person and returns a greeting to the person with that name. For example, if the input is "Larry", the function return value might be "Hello, Larry, how are you?". Test your function. Answer:
    function makeGreeting(firstName) {
        var greeting = "Hello, " + fname + ", how are you?";
        return greeting;
    }
    // Test the makeGreeting function:
    var greeting = makeGreeting("Alice");
    var greeting = makeGreeting("Larry");
    
    // Output:
    Hello, Alice, how are you?
    Hello, Larry, how are you?
    
  5. Write a JavaScript function that inputs a Fahrenheit temperature as a parameter and returns a descriptor for that temperature. Use the descriptors
    frigid  cold  chilly  perfect  warm  hot
    
    Use if..else statements to assign a descriptor to a temperature. Test your function. Answer:
    function descriptor(temp) {
        if (temp < 25) {
            return "frigid";
        }
        else if (temp < 50) {
            return "cold"
        }
        else if (temp < 65) {
            return "chilly";
        }
        else if (temp < 75) {
            return "perfect";
        }
        else if (temp < 95) {
            return "warm";
        }
        else {
            return "hot";
        }
    }
    
    console.log(descriptor(-7));
    console.log(descriptor(43));
    console.log(descriptor(62));
    console.log(descriptor(71));
    console.log(descriptor(89));
    console.log(descriptor(102));
    // Output:
    frigid
    cold
    chilly
    perfect
    warm
    hot
    

Practice Quiz 2b

Array Example

Expression Interpolation Example

Tutorial 2a