๐ Scenario: You have a list of prices for items in a store. You want to create a new list where each price is doubled, for example, to show the price after a special sale that doubles the cost.
๐ฏ Goal: Build a small program that uses the map method to double each number in an array of prices.
๐ What You'll Learn
Create an array called prices with the exact numbers: 10, 20, 30, 40, 50
Create a variable called multiplier and set it to 2
Use the map method on prices with a function that multiplies each price by multiplier
Store the result in a variable called doubledPrices
Print the doubledPrices array
๐ก Why This Matters
๐ Real World
Using the <code>map</code> method helps when you want to change every item in a list, like updating prices or adjusting scores.
๐ผ Career
Many programming jobs require working with lists of data and transforming them efficiently, and <code>map</code> is a common tool for this.
Progress0 / 4 steps
1
Create the prices array
Create an array called prices with these exact numbers: 10, 20, 30, 40, 50.
Javascript
Hint
Use square brackets [] to create an array and separate numbers with commas.
2
Create the multiplier variable
Create a variable called multiplier and set it to the number 2.
Javascript
Hint
Use const to create a variable that does not change.
3
Use map to double each price
Use the map method on the prices array with a function that multiplies each price by multiplier. Store the result in a variable called doubledPrices.
Javascript
Hint
Use array.map(item => item * multiplier) to create a new array with each item multiplied.
4
Print the doubled prices
Write a console.log statement to print the doubledPrices array.
Javascript
Hint
Use console.log(doubledPrices) to show the new array in the console.
Practice
(1/5)
1.
What does the map method do in JavaScript?
easy
A. Sorts the elements of the array in place.
B. Changes the original array by removing elements.
C. Creates a new array by applying a function to each element of the original array.
D. Filters elements from the array based on a condition.
Solution
Step 1: Understand the purpose of map
The map method applies a function to each element and creates a new array with the results.
Step 2: Compare with other array methods
Unlike filter or sort, map does not remove or reorder elements but transforms them.
Final Answer:
Creates a new array by applying a function to each element of the original array. -> Option C
Quick Check:
map creates new transformed array = A [OK]
Hint: Map always returns a new array with transformed items [OK]
Common Mistakes:
Thinking map changes the original array
Confusing map with filter or sort
Believing map removes elements
2.
Which of the following is the correct syntax to double each number in the array nums using map?
const nums = [1, 2, 3];
easy
A. const doubled = nums.map(x => x * 2);
B. const doubled = nums.map(x * 2);
C. const doubled = nums.map(x => { x * 2 });
D. const doubled = nums.map(x => return x * 2);
Solution
Step 1: Check arrow function syntax inside map
The function must be x => x * 2 to return the doubled value directly.
Step 2: Identify syntax errors in other options
const doubled = nums.map(x * 2); misses the arrow function syntax. const doubled = nums.map(x => { x * 2 }); uses braces but no return statement, so returns undefined. const doubled = nums.map(x => return x * 2); incorrectly uses return without braces.
Final Answer:
const doubled = nums.map(x => x * 2); -> Option A
Quick Check:
Correct arrow function syntax = B [OK]
Hint: Use arrow function with implicit return inside map [OK]
Common Mistakes:
Missing arrow (=>) in the function
Using braces without return statement
Incorrect return syntax inside arrow function
3.
What is the output of the following code?
const arr = [1, 2, 3];
const result = arr.map(x => x + 1);
console.log(result);
medium
A. [1, 2, 3]
B. [2, 3, 4]
C. [1, 3, 5]
D. Error
Solution
Step 1: Understand the map function applied
Each element x in arr is increased by 1, so 1 becomes 2, 2 becomes 3, and 3 becomes 4.
Step 2: Check the output array
The new array is [2, 3, 4], which is logged to the console.
Final Answer:
[2, 3, 4] -> Option B
Quick Check:
Each element +1 = [2, 3, 4] [OK]
Hint: Add 1 to each element inside map to get output [OK]