Get Programming with JavaScript - Listing 8.09
Listing 8.09 - Using the arguments passed by forEach
JS Bin
var players;
var showArguments;
players = [ "Dax", "Jahver", "Kandra" ];
showArguments = function (item, index, wholeArray) {
console.log("Item: " + item);
console.log("Index: " + index);
console.log("Array: " + wholeArray);
};
players.forEach(showArguments);
Further Adventures
Listing 8.09 - Using the arguments passed by forEach - Task 1
- Add two more names to the players array.
var players;
var showArguments;
players = [ "Dax", "Jahver", "Kandra", "Blinky", "Boo" ];
showArguments = function (item, index, wholeArray) {
console.log("Item: " + item);
console.log("Index: " + index);
console.log("Array: " + wholeArray);
};
players.forEach(showArguments);
Listing 8.09 - Using the arguments passed by forEach - Task 3
- Change the first console.log so it uses wholeArray and index to display the current item in the array. Don't use the item parameter.
var players;
var showArguments;
players = [ "Dax", "Jahver", "Kandra", "Blinky", "Boo" ];
showArguments = function (item, index, wholeArray) {
console.log("Item: " + wholeArray[index]);
console.log("Index: " + index);
console.log("Array: " + wholeArray);
};
players.forEach(showArguments);