Challenge - 5 Problems
Array Access Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
Accessing nested array elements
What is the output of the following code?
Javascript
const arr = [1, [2, 3], 4]; console.log(arr[1][0]);
Attempts:
2 left
π‘ Hint
Remember that arr[1] is an array itself, so you need to access its elements by index.
β Incorrect
arr[1] is the array [2, 3]. Accessing arr[1][0] gets the first element of that inner array, which is 2.
β Predict Output
intermediate2:00remaining
Accessing array elements with negative index
What will be the output of this code?
Javascript
const arr = [10, 20, 30]; console.log(arr[-1]);
Attempts:
2 left
π‘ Hint
JavaScript arrays do not support negative indices like some other languages.
β Incorrect
In JavaScript, negative indices do not access elements from the end. arr[-1] is undefined because no property '-1' exists.
β Predict Output
advanced2:00remaining
Accessing array elements after modification
What is the output of this code?
Javascript
const arr = [5, 10, 15]; arr[5] = 25; console.log(arr.length); console.log(arr[3]);
Attempts:
2 left
π‘ Hint
Setting an element at index 5 changes the length of the array.
β Incorrect
Assigning arr[5] = 25 sets the sixth element (index 5), so length becomes 6. arr[3] was never set, so it is undefined.
β Predict Output
advanced2:00remaining
Accessing array elements with destructuring
What will be logged by this code?
Javascript
const arr = [100, 200, 300]; const [a, , c] = arr; console.log(a, c);
Attempts:
2 left
π‘ Hint
Destructuring skips the second element with the empty comma.
β Incorrect
The destructuring assigns a = arr[0] = 100, skips arr[1], and assigns c = arr[2] = 300.
β Predict Output
expert2:00remaining
Accessing array elements with dynamic keys
What is the output of this code?
Javascript
const arr = [1, 2, 3]; const key = '1'; console.log(arr[key]);
Attempts:
2 left
π‘ Hint
Array indices are converted to strings internally.
β Incorrect
JavaScript arrays use string keys for indices. arr['1'] accesses the element at index 1, which is 2.