Assignment 4 (pdf)

Make sure to test all your answers in the console.

  1. Write a function that takes as arguments an array arr and a function g, and returns the result of applying g to each element in the array and adding the resulting values. Do not use any array methods other than array indexing and array length.

  1. Write a function makeAlternate that takes no arguments, and when called returns a function. That returned function when called returns true one time, false the next time, then back to true and so on, alternating between true and false.

  1. Write a function makeCounter that is similar to the one we introduced in class, but it also accepts an argument init, and the counter it returns will start counting from init instead of 1. (So if var a = makeCounter(10), then a() would equal 10 the first time and 11 the second time and so on from there).

  1. Consider the following code:

    var a = 2;
    function foo(a) { a = 6; }
    foo();
    a;

    What is the value of the variable a from the last line? How do you explain that?