0
0
Javascriptprogramming~20 mins

Array creation in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Array Creation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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);
A[2, 4, 6, 8]
B[0, 1, 2, 3]
C[0, 2, 4, 6]
D[undefined, undefined, undefined, undefined]
Attempts:
2 left
πŸ’‘ Hint
Array.from creates an array from an object with length and applies the function to each index.
❓ Predict Output
intermediate
2: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);
A[1, 2, 3]
B[2, 3, 4]
C[1, 1, 1]
D[NaN, NaN, NaN]
Attempts:
2 left
πŸ’‘ Hint
fill sets all elements to 1, then map adds the index to each element.
❓ Predict Output
advanced
2: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);
A[]
B[0, 1, 2, 3, 4]
C[undefined, undefined, undefined, undefined, undefined]
D[1, 2, 3, 4, 5]
Attempts:
2 left
πŸ’‘ Hint
Array(5).keys() returns an iterator of indexes from 0 to 4.
❓ Predict Output
advanced
2: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);
A1
B3
CTypeError
Dundefined
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.
🧠 Conceptual
expert
2: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?
AArray.from({ length: 4 }, () => 0)
B[0, 0, 0, 0]
Cnew Array(4).map(() => 0)
DArray(4).fill(0)
Attempts:
2 left
πŸ’‘ Hint
map on a new Array with empty slots does not run the function.