Make sure to test all your answers in the console.
arr.reduce(f, a)
to compute the product of the values in the array arr
. What should the values of f
and a
be?
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.
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.
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).