Recall & Review
beginner
What does the
map() method do in JavaScript?The
map() method creates a new array by applying a function to each element of the original array.Click to reveal answer
beginner
How does
map() differ from forEach()?map() returns a new array with transformed elements, while forEach() just runs a function on each element without returning anything.Click to reveal answer
intermediate
What arguments does the callback function inside
map() receive?The callback receives three arguments: the current element, the index of the element, and the original array.
Click to reveal answer
intermediate
Can
map() change the size of the array it returns?No,
map() always returns a new array of the same length as the original array.Click to reveal answer
beginner
Give an example of using
map() to double numbers in an array.Example: <br><code>const numbers = [1, 2, 3];<br>const doubled = numbers.map(n => n * 2);<br>console.log(doubled); // [2, 4, 6]</code>Click to reveal answer
What does the
map() method return?✗ Incorrect
map() always returns a new array where each element is the result of the callback function.Which of these is a valid use of
map()?✗ Incorrect
map() transforms each element and returns a new array of the same length.What arguments does the callback function in
map() receive?✗ Incorrect
The callback receives the current element, its index, and the original array.
If you want to change the size of an array, is
map() the right method?✗ Incorrect
map() always returns an array with the same number of elements as the original.What will this code output? <br>
[1, 2, 3].map(x => x + 1)✗ Incorrect
Each element is increased by 1, so the new array is [2, 3, 4].
Explain how the
map() method works and when you would use it.Think about turning a list of raw items into a list of changed items.
You got /4 concepts.
Describe the difference between
map() and forEach() in JavaScript.One returns a new list, the other just runs code on each item.
You got /4 concepts.