Get Programming with JavaScript - Listing 8.08
Listing 8.08 - Calling forEach with an inline function
JS Bin
var items = [ "The Pyramids", "The Grand Canyon", "Bondi Beach" ];
console.log("Dream destinations:");
items.forEach(function (item) {
console.log(" – " + item);
});
Further Adventures
Listing 8.08 - Calling forEach with an inline function - Task 1
- Change the program to show the number of places to visit as well as the places themselves.
var items = [ "The Pyramids", "The Grand Canyon", "Bondi Beach" ];
console.log("There are " + items.length + " dream destinations:");
items.forEach(function (item) {
console.log(" – " + item);
});
Listing 8.08 - Calling forEach with an inline function - Task 2
- Wrap the display code in a function and assign it to the showItems variable.
var items = [ "The Pyramids", "The Grand Canyon", "Bondi Beach" ];
var showItems = function () {
console.log("There are " + items.length + " dream destinations:");
items.forEach(function (item) {
console.log(" – " + item);
});
};