garyfitz
3/20/2018 - 8:41 AM

.forEach() Iterator

.forEach() example iterator

let groceries = ['whole wheat flour', 'brown sugar', 'salt', 'cranberries', 'walnuts']; 

groceries.forEach(function(groceryItem) {
  console.log(' - ' + groceryItem);
});

// arrow function syntax of this
groceries.forEach(groceryItem => console.log(' - ' + groceryItem));


let animals = ['Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog'];

// Create the secretMessage array below
//let secretMessage = animals.map(function(animal){
    //return animal[0];
//});

let secretMessage = animals.map(animal => animal.charAt(0));

console.log(secretMessage.join(''));

let bigNumbers = [100, 200, 300, 400, 500];

// Create the smallNumbers array below
//let smallNumbers = bigNumbers.map(function(number){
 // return number / 100;
//});

let smallNumbers = bigNumbers.map(number => number / 100);