When should I use .map()? When should I use a for loop?

Question

When should I use .map()? When should I use a for loop?

Answer

Use .map() when you want to create a new array of transformed values of the original array by iterating over and calling a function on every element of said original array. Use a for loop when you need a less specialized and more flexible iterator.

3 Likes

In this exercise, it is correct if I utilize both for loops or .map right?

1 Like

In this exercise, it is correct if I utilize both for loops or .map right?

Can you share your .for loop idea?

When would I use .forEach() when .map()?

1 Like

@jnijnijni Yes you can use for loop, .forEach() , .map() and even a while loop!

@bsanto187 here’s a for loop solution:

// using for loop
function shoutGreetings2(strArray){
let newArray = [ ]
for (let i=0; i< strArray.length; i++){
newArray.push(strArray[i].toUpperCase() + “!”)
}
return newArray
}

:slightly_smiling_face:

3 Likes

@blogwhiz20587

The default return value of the .forEach() iterator is undefined while the default return value for .map() is an Array. So if you expect to return an array it’s best to use .map() since you’ll need to write fewer lines of code and thus chances of error are somewhat reduced.
Below is a comparison of for, forEach() and .map()

// using .map()

function shoutGreetings(strArray){

let greet = strArray.map((greeting)=> greeting.toUpperCase() + “!”)

return greet

}

// using for loop

function shoutGreetings2(strArray){

let newArray =

for (let i=0; i< strArray.length; i++){

newArray.push(strArray[i].toUpperCase() + "!")

}

return newArray

}

// using forEach()

function shoutGreetings3(strArray){

let newArray =

strArray.forEach((greeting)=>{

newArray.push(greeting.toUpperCase() + "!")

})

return newArray

}

hope that helps :slightly_smiling_face:

12 Likes

There are many ways you can achieve something. However, you should try to utilize the tools that JavaScript provides which makes coding easier and is more appropriate for the context. I think if we want to loop over arrays, we should use iterators

you can simplify your map even further by doing this

 function shoutGreetings(arr){
  return arr.map(ele=>ele.toUpperCase()+'!')
}
4 Likes

Cool Thanks

// Write your code here: const shoutGreetings = arr => { const arrs = []; for ( let i = 0; i < arr.length; i++){ arrs.push(arr[i].toUpperCase() + "!"); } return arrs; } // Feel free to uncomment out the code below to test your function! const greetings = ['hello', 'hi', 'heya', 'oi', 'hey', 'yo']; console.log(shoutGreetings(greetings)) // Should print [ 'HELLO!', 'HI!', 'HEYA!', 'OI!', 'HEY!', 'YO!' ]

This was a really great example and it really points out how there are many different ways to solve any problem. In this case, using .map() results in a lot less code.

In fact, I think this can be simplified even further to one line:

const shoutGreetings = arr => arr.map(elem => elem = `${elem.toUpperCase()}!`);
3 Likes

const shoutGreetings = arr => {
let newArr =
for (let i = 0; i < arr.length; i++) {
newArr.push(arr[i] + ‘!’);
}
return newArr;
};
I originally done it with .map() but this syntax works too :grin:

thank you so much; i blanked on the push method!