DEV Community

Discussion on: What’s your alternative solution? Challenge #6

Collapse
westdabestdb profile image
Görkem • Edited
let prod = 1;
[...Array(10).keys()].map(x => {
  ++x;
  prod *= x;
});
console.log(prod);
Collapse
lukaszahradnik profile image
Lukáš Zahradník • Edited

This is an example of wrong usage of map.

Collapse
13point5 profile image
Bharath Sriraam R R

May I know what you mean by "wrong"? If you mean inefficient please do elaborate, I would like to hear about it!

Thread Thread
lukaszahradnik profile image
Lukáš Zahradník • Edited

Well, it is inefficient as well.

In this example the map is just exploited for iterating over elements and completely ignores purpose of the function (transforming array to a new array). It would be better to use forEach or reduce instead.

Thread Thread
13point5 profile image
Bharath Sriraam R R

Good point, thank you!

Collapse
aleksandrhovhannisyan profile image
Aleksandr Hovhannisyan

This takes a very simple problem and either intentionally or unintentionally obfuscates it. Why?