Quiz

  1. Still not too sure about the meaning of first class items.

    The phrase first class means it can be stored in variables, passed as values, etc. In JavaScript, it's common to pass a function as an argument to another function/method.

  2. difference between global and local variables

    Global variables are shared among all functions. Useful, but require coordination among functions (possibly authored by different people) and can conflict (two people want to use x for different purposes).

    Local variables solely belong in a particular scope (typically a function), and aren't shared at all.

    
        function someFunction(a,b) {
            var x = a + b;   // variable local to this function
            ...
        }
    
    
  3. So var is used to declare a local variable, then how do we declare a global variable?

    Strangely, you *omit* the var when you refer to it in a function:

    
        var numStudents = 30;
        var roster = [];
        function addStudent(name) {
            roster.append(name);
            numStudents ++;
        }
    
    
  4. Anonymous functions / Can you please review anonymous functions?

    Anonymous functions work the same as named functions:

    
        var f = function (x) { return x+3; }
        function g(x) { return x+3; }
        alert(f(7));
        alert(g(7));
    
    

    Where it gets interesting is a function as an argument:

    
        function demo_function(func, x) {
            var y = func(x);
            alert("on input "+x+" the function returns "+y);    
        }
        demo_function(f, 7);
        demo_function(g, 7);
        demo_function(function (x) { return x+44; }, 7);
    
    

    The last example passes in a function without first storing it or even naming it.

  5. Do named functions and anonymous functions differ by the amount of storage that they take up?

    Not in any significant way. Yes, the browser needs to store the name of the function, but that's a trivial amount.

  6. Why do you need anonymous functions? Why couldn't you do the arithmetic without a function?

    It's not the arithmetic that is the point; that's just to keep the functions simple.

    The point is that we often need to create a function solely to give it to the browser so that it can call the function when some event occurs. That's called event-handling, and it's how we do web programming.

    It's often convenient just to create the function anonymously and pass it immediately to the browser. Even if you never do this, it's standard practice in web programming, so you need to wrap your head around it.

  7. Is there a reason that websites use JavaScript and not Python?

    Because when the first interactive browser was being created (Netscape), he wanted to put Scheme in the browser (not Python) but his bosses insisted that it have Java-like syntax. See Netscape and JavaScript. He completed the first version in ten days!!!.

  8. Can you go over creating functions and using them directly in the HTML? Thanks!

    We've seen a few examples already. We can open the console and try a few more.

    
    function hyp(a,b) {
        return Math.sqrt(a*a + b*b);
    }
    hyp(3,4);
    
    
  9. could we go over the forEach method, thank you! can you review the forEach method? How does it work (whats its output)?

    It runs a function (which is an argument) on each of the values in the array.

    The method returns no value, and the function doesn't need to return a value.

    
    // this Gandhi quote is abbreviated 
    var quote = ['Be', 'the', 'change', 'you', 'want'];
    quote.forEach( function (str) { alert(str.toUpperCase()); } );
    quote.forEach( function (str, index) { alert(index + ': ' + str.toUpperCase()); } )
    
    
  10. Could you explain the forEach Method more? Why is the third argument (the array itself) ignored?

    It's not ignored, but it's rarely used. Here's a case where it is:

    
    quote.forEach( function (str, index, arr) { arr[index] = str.toUpperCase(); } )
    
    
  11. Can anonymous functions be used multiple times? Or does the information within the function "reset" after it has been declared the first time?

    Yes. We just saw a bunch of examples where the function passed to forEach is called five times (once for each word in the quote).

    Functions *always* start over ("reset"), whether they are anonymous or named. That's true in Python and all other languages. (That's not perfectly true, but close enough.)

  12. I don't think I understand the point of the arrow function. Why use it at all?

    It's really succinct:

    
    nums = [ 3, 1, 4, 1, 5 ];
    nums.map( x => x*x );
    nums.map( function (x) { return x*x });
    
    

    They have some other important differences which we'll talk about later, when we get to object-oriented programming (OOP).

  13. I'm still confused about the difference between let and var. Can you do an example in class?

    The difference is small and subtle. If the difference matters to your code, you're probably doing something wrong. Here's a brief example:

    
    function foo(a) {
        // here, x exists but is undefined. y doesn't exist
       alert(x);
       if( a > 0 ) {
            var x = Math.sqrt(a);
            let y = Math.cos(a);
        }
        // here, x exists and is defined. y doesn't exist
        alert(x);
        alert(y); // referring to y will produce an error
    }
    
    

    If you want to hear more, let's talk in office hours, but you truly should not worry about this.

  14. JavaScript as an object-oriented language vs. other object-oriented languages.

    We'll talk about OOP in a few weeks.