careerbydoing
← Web development

Web development

Transforming a list, JavaScript-style

About 2 min if you have seen this material before, 8 min or so if it is new to you.

Background
Where Python writes a comprehension, JavaScript calls a method and hands it a function: a A function that takes another function as an argument, like JavaScript's map or filter.. So [x * 2 for x in xs] becomes xs.map(x => x * 2), where x => x * 2 is an arrow function, JavaScript's compact lambda. The three you will meet constantly are map, filter, and reduce. The distinction that trips people up is what each does to the *length* of the result, and that is what this problem is about. Note also that length is a property, not a method: no parentheses, unlike Python's len(xs).
What does this print?
const xs = [1, 2, 3];
console.log(xs.map((x) => x * 2).length);

Past 25 min on this? That usually means a missing prerequisite rather than a lack of effort — worth reading up before pushing further, starting with Array.prototype.map() (MDN).

Want to go deeper?