What is the difference between the output of .map and .forEach


#1

First of all I am unable to find Javascript iterators when pulling up the additional topic.

Here is the exercise:
<a href='https://www.codecademy.com/courses/learn-javascript-iterators/lessons/javascript-iterators/exercises/iterators-review?action=lesson_resume&course_redirect=introduction-to-javascript>barefoot!

Also, how do I properly format the link above?

Now the real issue:

Here is .map():

There are different animals that make up an array, which is posted directly below this line
let animals = ['Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog'];

let secretMessage = animals.map(function(animal) {
return animal.charAt(0);});

Here is .ofEach():

let fruits = ['mango', 'papaya', 'pineapple', 'apple'];

// Iterate over fruits below
fruits.forEach(function(fruitItem) {console.log('-' + fruitItem);});

I’m making sure I understand the concept!


#2

Let me be clear, I was hoping to use .forEach on the first one (that uses .map) and .map on the second one, that uses .forEach.


#3

have you checked documentation? forEach will simple give you each value in the list (through a callback function), which you can do something with

.map() is similair, but it constructs a new array (with the returned result of the callback function). forEach only loops, it doesn’t create an array.


#4

A post was split to a new topic: Map and reduce application


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.