0
0
Javascriptprogramming~20 mins

Common array operations in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Array Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Array filter and map combination
What is the output of the following JavaScript code?
Javascript
const numbers = [1, 2, 3, 4, 5];
const result = numbers.filter(n => n % 2 === 0).map(n => n * 3);
console.log(result);
A[3, 9, 15]
B[6, 12]
C[2, 4]
D[1, 3, 5]
Attempts:
2 left
πŸ’‘ Hint
Remember filter keeps only even numbers, then map multiplies each by 3.
❓ Predict Output
intermediate
2:00remaining
Result of Array reduce for sum
What is the value of total after running this code?
Javascript
const values = [10, 20, 30];
const total = values.reduce((acc, val) => acc + val, 5);
console.log(total);
A60
BSyntaxError
C30
D65
Attempts:
2 left
πŸ’‘ Hint
The initial value for reduce is 5, so sum starts from 5.
❓ Predict Output
advanced
2:00remaining
Output of Array splice method
What will be logged to the console after running this code?
Javascript
const arr = [1, 2, 3, 4, 5];
const removed = arr.splice(2, 2, 8, 9);
console.log(arr);
console.log(removed);
Aarr: [1, 2, 8, 9], removed: [5]
Barr: [1, 2, 3, 4, 5], removed: []
Carr: [1, 2, 8, 9, 5], removed: [3, 4]
Darr: [8, 9, 5], removed: [1, 2, 3, 4]
Attempts:
2 left
πŸ’‘ Hint
splice removes 2 elements starting at index 2 and inserts 8 and 9 there.
❓ Predict Output
advanced
2:00remaining
Output of Array sort with compare function
What is the output of this code?
Javascript
const fruits = ['banana', 'apple', 'cherry'];
fruits.sort((a, b) => b.localeCompare(a));
console.log(fruits);
A['cherry', 'banana', 'apple']
B['apple', 'banana', 'cherry']
C['banana', 'apple', 'cherry']
DTypeError
Attempts:
2 left
πŸ’‘ Hint
localeCompare returns positive if a is after b alphabetically. b.localeCompare(a) sorts descending.
🧠 Conceptual
expert
2:00remaining
Which option causes a runtime error?
Which of the following array operations will cause a runtime error when executed?
Aconst arr = [1, 2, 3]; arr.forEach(x => { if(x > 1) throw new Error('Stop'); });
Bconst arr = [1, 2, 3]; const filtered = arr.filter(x => x > 1);
Cconst arr = [1, 2, 3]; const result = arr.map(x => x * 2);
Dconst arr = [1, 2, 3]; const first = arr.find(x => x > 3);
Attempts:
2 left
πŸ’‘ Hint
Look for explicit error throwing inside the callback.