Challenge - 5 Problems
Array Access Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this code after updating the array?
Consider the following JavaScript code that updates an array element at a specific index. What will be printed after the update?
DSA Javascript
const arr = [10, 20, 30, 40, 50]; arr[2] = 99; console.log(arr.join(' -> ') + ' -> null');
Attempts:
2 left
💡 Hint
Remember that array indices start at 0, so arr[2] refers to the third element.
✗ Incorrect
The code changes the element at index 2 (third element) from 30 to 99. The rest of the array remains unchanged.
❓ Predict Output
intermediateWhat is the output after multiple updates on the array?
Look at this JavaScript code that updates multiple elements in an array. What will be printed?
DSA Javascript
const arr = [5, 10, 15, 20, 25]; arr[0] = 50; arr[4] = 100; console.log(arr.join(' -> ') + ' -> null');
Attempts:
2 left
💡 Hint
Check which indices are updated and what values they get.
✗ Incorrect
The first element (index 0) is changed from 5 to 50, and the last element (index 4) is changed from 25 to 100.
❓ Predict Output
advancedWhat happens when updating an index outside the array length?
Consider this JavaScript code that updates an array at an index beyond its current length. What will be printed?
DSA Javascript
const arr = [1, 2, 3]; arr[5] = 10; console.log(arr.join(' -> ') + ' -> null');
Attempts:
2 left
💡 Hint
Think about what happens to the array length and missing elements when assigning beyond current length.
✗ Incorrect
Assigning to index 5 extends the array length to 6. Missing elements (index 3 and 4) become undefined.
🔧 Debug
advancedWhat error does this code produce when updating an array?
Look at this JavaScript code snippet. What error will it produce when run?
DSA Javascript
const arr = [1, 2, 3]; arr[1.5] = 10; console.log(arr.join(' -> ') + ' -> null');
Attempts:
2 left
💡 Hint
Remember that array indices must be integers; what happens if you use a decimal?
✗ Incorrect
Using a non-integer index adds a property to the array object but does not change its length or elements.
🧠 Conceptual
expertHow many elements does the array have after this update?
Given the following JavaScript code, how many elements does the array contain after the update?
DSA Javascript
const arr = [0, 1, 2]; arr[10] = 100;
Attempts:
2 left
💡 Hint
Array length updates to one more than the highest index assigned.
✗ Incorrect
Assigning to index 10 sets array length to 11 (indices 0 to 10). Missing elements are empty slots.
