0
0
Javascriptprogramming~20 mins

Iterating over objects in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Object Iteration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of iterating object keys with for...in
What is the output of the following code?
Javascript
const obj = {a: 1, b: 2, c: 3};
let result = '';
for (const key in obj) {
  result += key;
}
console.log(result);
A"abc"
B"123"
C"a1b2c3"
D"undefined"
Attempts:
2 left
πŸ’‘ Hint
for...in loops over keys of an object.
❓ Predict Output
intermediate
2:00remaining
Output of Object.entries iteration
What is the output of this code snippet?
Javascript
const obj = {x: 10, y: 20};
let sum = 0;
for (const [key, value] of Object.entries(obj)) {
  sum += value;
}
console.log(sum);
A30
Bxy
Cundefined
D1020
Attempts:
2 left
πŸ’‘ Hint
Object.entries returns key-value pairs as arrays.
❓ Predict Output
advanced
2:00remaining
Output of iterating object with Object.keys and map
What will this code output?
Javascript
const obj = {a: 1, b: 2, c: 3};
const result = Object.keys(obj).map(key => obj[key] * 2);
console.log(result);
A["a2", "b4", "c6"]
BTypeError
C[1, 2, 3]
D[2, 4, 6]
Attempts:
2 left
πŸ’‘ Hint
Object.keys returns keys, map transforms values.
❓ Predict Output
advanced
2:00remaining
Output of for...of with Object.values
What is the output of this code?
Javascript
const obj = {p: 5, q: 10};
let output = '';
for (const val of Object.values(obj)) {
  output += val + '-';
}
console.log(output);
A"510"
B"pq"
C"5-10-"
DSyntaxError
Attempts:
2 left
πŸ’‘ Hint
Object.values returns values only.
❓ Predict Output
expert
2:00remaining
Output of nested object iteration with destructuring
What is the output of this code?
Javascript
const data = {a: {x: 1}, b: {x: 2}};
let sum = 0;
for (const {x} of Object.values(data)) {
  sum += x;
}
console.log(sum);
ATypeError
B3
CNaN
Dundefined
Attempts:
2 left
πŸ’‘ Hint
Object.values returns nested objects, destructuring extracts x.