Assignment 5 (pdf)

Make sure to test all your answers in the console.

  1. We want the expression arr.reduce(f, a) to compute the product of the values in the array arr. What should the values of f and a be?

  1. We have an array arr of numbers. We want to obtain a new array containing only those numbers that are positive. Write an expression that will do that, using one of the new methods we learned.

  1. We have an array arr of strings. We want to produce a new array that wraps each string around the tags “<li>” and “</li>”. Write an expression that will do that, using one of the new methods we learned.

  1. Consider the following code:
var A = [], B = [];
for (var i = 0; i < 5; i += 1) {
    A[i] = function() {  return i; };
    B[i] = (function(i) {
                return function() { return i; };
            })(i);
}
A.forEach(function(f, ind) { console.log("A", ind, f()); });
B.forEach(function(f, ind) { console.log("B", ind, f()); });

Describe the output produced by this code, and explain the unusual behavior (You should make sure you run this code in the console).