Challenge - 5 Problems
Array Creation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
Output of Array.from with mapping function
What is the output of this JavaScript code?
Javascript
const arr = Array.from({ length: 4 }, (_, i) => i * 2); console.log(arr);
Attempts:
2 left
π‘ Hint
Array.from creates an array from an object with length and applies the function to each index.
β Incorrect
Array.from creates an array of length 4. The mapping function multiplies each index by 2, so the array is [0, 2, 4, 6].
β Predict Output
intermediate2:00remaining
Output of new Array with fill and map
What is the output of this JavaScript code?
Javascript
const arr = new Array(3).fill(1).map((x, i) => x + i); console.log(arr);
Attempts:
2 left
π‘ Hint
fill sets all elements to 1, then map adds the index to each element.
β Incorrect
new Array(3) creates an array with 3 empty slots. fill(1) sets all to 1. map adds index to each element: 1+0=1, 1+1=2, 1+2=3.
β Predict Output
advanced2:00remaining
Output of spread operator with Array constructor
What is the output of this JavaScript code?
Javascript
const arr = [...Array(5).keys()]; console.log(arr);
Attempts:
2 left
π‘ Hint
Array(5).keys() returns an iterator of indexes from 0 to 4.
β Incorrect
Array(5) creates an array with 5 empty slots. keys() returns an iterator over indexes 0 to 4. Spread operator converts iterator to array [0,1,2,3,4].
β Predict Output
advanced2:00remaining
Output of Array constructor with string argument
What is the output of this JavaScript code?
Javascript
const arr = new Array('3'); console.log(arr.length);
Attempts:
2 left
π‘ Hint
A single numeric string argument like '3' to the Array constructor is converted to number 3 and used as the array length.
β Incorrect
new Array('3') creates an array with one element '3' because the argument is a string, not a number.
π§ Conceptual
expert2:00remaining
Which option creates an array of length 4 filled with zeros?
Which of the following JavaScript code snippets creates an array of length 4 where every element is the number 0?
Attempts:
2 left
π‘ Hint
map on a new Array with empty slots does not run the function.
β Incorrect
Array(4).fill(0) creates an array of length 4 filled with zeros. [0,0,0,0] is also correct but is a literal, not a creation method. new Array(4).map(() => 0) returns an array of 4 empty slots because map does not run on empty slots. Array.from works but the question asks for the method that creates it directly.