0
0
Javascriptprogramming~20 mins

Map method in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Map Method Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this map method code?
Consider the following JavaScript code using the map method. What will be printed to the console?
Javascript
const numbers = [1, 2, 3, 4];
const doubled = numbers.map(x => x * 2);
console.log(doubled);
A[1, 2, 3, 4]
B[2, 4, 6, 8]
C[NaN, NaN, NaN, NaN]
DTypeError
Attempts:
2 left
πŸ’‘ Hint
Remember, map creates a new array by applying the function to each element.
❓ Predict Output
intermediate
2:00remaining
What does this map method return?
Look at this code snippet. What will be the value of result after running it?
Javascript
const words = ['apple', 'banana', 'cherry'];
const result = words.map(word => word.length);
console.log(result);
A[5, 6, 6]
B["apple", "banana", "cherry"]
C["a", "b", "c"]
DReferenceError
Attempts:
2 left
πŸ’‘ Hint
The function returns the length of each word.
πŸ”§ Debug
advanced
2:00remaining
Why does this map code produce an error?
This code tries to double numbers using map, but it causes an error. What is the cause?
Javascript
const nums = [1, 2, 3];
const doubled = nums.map(x => { x * 2 });
console.log(doubled);
AThe arrow function lacks a return statement, so it returns undefined for each element.
BThe array is empty, so map cannot run.
CThe map method is not defined on arrays.
DThe multiplication operator * is invalid here.
Attempts:
2 left
πŸ’‘ Hint
Arrow functions with curly braces need an explicit return to output a value.
❓ Predict Output
advanced
2:00remaining
What is the output of this map with object transformation?
What will this code print to the console?
Javascript
const users = [{name: 'Anna', age: 25}, {name: 'Ben', age: 30}];
const names = users.map(user => user.name.toUpperCase());
console.log(names);
A[{name: "ANNA"}, {name: "BEN"}]
BTypeError
C["anna", "ben"]
D["ANNA", "BEN"]
Attempts:
2 left
πŸ’‘ Hint
The function converts each user's name to uppercase.
🧠 Conceptual
expert
2:00remaining
Which option correctly explains the behavior of map with sparse arrays?
Given a sparse array const arr = [1, , 3]; (note the missing element), what does arr.map(x => x * 2) produce?
A[2, NaN, 6]
B[2, undefined, 6]
C[2, , 6]
DThrows a TypeError
Attempts:
2 left
πŸ’‘ Hint
Map skips missing elements in sparse arrays and keeps holes.