Quiz

  1. I think it could be useful to see more instances of first-class functions and common JS syntax misconceptions (one example mentioned in the reading was string concatenation from user input eg user inputs 7 and 9 to find sum, but user input is automatically converted to a string before concatenation).

    Sure. We'll see a lot of first-class functions in the Plotting assignment (that's the theme of the assignment). We'll see some today as well.

    As for concatenation, here's the idea. There's nothing obviously wrong with the following code:

    
        let age = prompt("How old are you?")
        let older = age+1;
        console.log("Next year, you will be "+older+"!!");
    
    

    In Python, you would get a complaint about age+1.

    Other situations where this arises is in searching a database. If the keys are numbers and your value is a string, it won't necessarily work, and printing the values will not make it any clearer.

  2. Why does the forEach function know that str refers to each element, index refers to the current index, and arr refers to the entire array in this code?
    
    let quote = ['be', 'the', 'change', 'you', 'want'];
    quote.forEach(function (str, index, arr) { arr[index] = str.toUpperCase(); });
    console.log(quote);  // ['BE', 'THE', 'CHANGE', 'YOU', 'WANT']
    
    
    And if we are given an array of integers, should we change str to item/element?

    The answer to your first question is that those are the rules of the game. It's like asking the following:

    
        function diff(x, y) { return x - y };
        diff(3,4);   // how does JS know that x is 3 and y is 4?
    
    

    The answer to your second question is definitely yes. We should name our variables intuitively, so that someone reading the code will find it easier to read.

    Maybe word would have been better than str above?

  3. can we do one example of Arrow Functions in class?

    Sure! Let's do one now:

    
        let diff = (x,y) => x - y;
        [1, 2, 3, 4].map( val => diff(val,3) );
    
    
  4. Practice with anonymous functions and forEach.

    We'll do some, though we will focus on map rather than forEach. They are mostly similar (iterate over an array, invoking the callback function on each element), but slightly different (map accumulates results into a new array, while forEach does not).

  5. Is it possible to practice how to write some of these codes in class?

    Yes, we will.

  6. Material seems pretty straightforward! Maybe we could talk a bit more about .forEach() and its arguments?

    Sure. Most of the time, we are only interested in the value of the array element, so we use the one-argument callback:

    
    function biggest(numbers) {
        let max = -1000;
        numbers.forEach( (val) => { if(val>max) max = val })
        return max;
    }
    biggest([3, 1, 4, 1, 5, 2, 6, 9, 8, 3, 7]);
    
    

    But sometimes, we care about the index as well, and we use the two-argument callback:

    
    function biggestIndex(numbers) {
        let max = -1000; 
        let maxIndex = -1;
        numbers.forEach( (val,idx) => { if(val>max) { max = val; maxIndex = idx; } });
        return maxIndex;
    }
    biggestIndex([3, 1, 4, 1, 5, 2, 6, 9, 8, 3, 7]);
    
    
  7. Although it won’t be covered in class, I’m curious about the different types of loops in JavaScript.

    Let's look briefly at loops in JS

  8. This reading about JS basics is very clear! / All good for now! / all good!

    Great!

  9. Not exactly from the reading, but I just downloaded firefox to use the console. Every few times I entered things pasted from the reading, it displayed the results but also gave the error "Source map error: Error: URL constructor: is not a valid URL.". What does this mean?

    Hmm. I'm not sure. I found this about source map error on Stack Overflow.