Quiz

  1. Maybe some examples of how we might use anonymous functions when designing a website. Most of the examples from the readings seemed to focus on equations, and I wonder if we'll be using anonymous functions in other ways.

    Good question. In about a week, we'll be creating functions, often anonymous ones, to hand to the browser to be invoked by the browser when events occur. These functions will be handed to the browser by having them be the arguments of functions defined by the browser (or by a helper library).

  2. I actually had a question about question 3. Does the answer choice "A. functions can be the value of a function" mean that the function can return another function? or does it mean that it can be the argument of another function?

    Both.

    A function can be the value of another function. The function f can return a function g (or an anonymous function with the same behavior).

    A function can also be the argument of another function. A function h can take a function as its argument. Maybe the g that was returned by f

  3. What does it mean by function being a value? Isn't function's result being the value we are getting? If we have anonymous function, can that also be a value?

    A function is an object that can be return and passed, like numbers, strings, dictionaries, etc.

    Yes, anonymous function can be values.

    
        function makeDie(sides) {
            return function () { return 1+Math.floor(Math.random()*sides) }
        }
    
    
    
  4. function as a first class values x 5

    This is an important idea in web development. See the first question.

  5. Could I have multiple layers of functions? (if that makes sense). For example: a function which takes in a function as a parameter but that function also takes in another function as its parameter.

    Yes. In fact, later in the course, we will talk about "callback hell," which happens when you have many layers like that. Fortunately, there were innovations that greatly reduced callback hell.

  6. I still don't really understand how anonymous functions work and why they are necessary. / Overview of why anonymous functions are useful

  7. Does it matter which browser we use for testing code in the browser’s JS console? Are there differences between the browser consoles for Chrome, Firefox, DuckDuckGo, etc?

    Not appreciably. I prefer Firefox, and I encourage you to use it, but....

  8. Can map and foreach be used interchangeably in some cases? / Difference between for each and map. / When to use map vs. forEach

    They are quite similar, but different.

    map collects and returns an array of the values;

    forEach doesn't return anything.

    So, map is more like a list comprehension, while forEach is like a for loop.

  9. so the map function iterates over every element in a list and performs the function given as an argument on every element... does this work for things like dictionaries or multidimensional arrays as well?

    Multidimensional arrays are often represented as nested arrays, in which case, yes, you can map across any dimension.

    dictionaries aren't sequences, but their keys are, so we often use forEach and map on the keys of a dictionary:

    
    Object.keys(someDict).map( (key) => "key"+key+" maps to "+someDict[key] );
    
    

    Note the use of the square bracket notation. Can't use "." there

  10. Is there a way to invoke an argument function for .foreach(func) with the item and the array (without the index) using two arguments?

    I'm not sure why you'd want to. Why have the array w/o an index?

    You an use the three-arg version and just ignore the second arg:

    
    someArray.forEach( (elt, index, array) => console.log(elt," is in ", array));
    
    
  11. 1. In the scope of this class, would we later use ""var"" instead of ""let""?

    Either is fine.

  12. What is the difference between the keywords var vs const and let? What does var do?

    let and var are storage locations that are read/write. const is a storage location that is read-only

    "In hell, variables don't and constants aren't".

  13. Also, when we refer other functions from external JS file, would it be okay if the values were defined as either let or var? Is variables at the external considered local (like current JS and external JS file just counted as one if called in)?

    Both let and var are fine.

    All the JS files go into one namespace bucket, unless we use the new module system, which we won't. For now.

  14. 3. Is it correct that functions can't be defined with const? Or since the function logic don't change we can consider it const variable?

    No; functions can be declared with const. In fact, they probably should be.

  15. Is JavaScript the standard language used for developing web-based applications? Where else is JavaScript used?

    On the server, you can use any language you want, including JavaScript

    In the browser, you can use any language you want, as long as its JavaScript

  16. Is the JS console just a fun tool we can use to run JS code to see how it works or is it actually (and typically) what is used by programmers to make modifications to the browser webpage (or is that done with the .js file in the html) ? It seems like all changes (variables defined, etc.) are gone as soon as we refresh the page.

    It is a serious, industrial strength language, running in the browser.

    Yes, all changes are local to the browser tab, and disappear when the browser is refreshed.

    For long-term persistence, we need a back-end database. We'll learn how to do that.

  17. How to connect javascript and HTML scripts! Essentially, I would like more practice doing it and to see some examples.

    We will!

  18. arrow functions; and I am still a bit confused about the alert command

    Arrow functions are a shorthand for the longer syntax. They have some subtle differences that won't affect us.

    I'll demo alert

  19. How are NaN and null different?

    NaN is "Not a Number" — a special object used when calculations go awry.

    null is like Python None and Java's and C's null

  20. Made all sense! / Everything was pretty clear for me!