Challenge - 5 Problems
Object Iteration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2: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);
Attempts:
2 left
π‘ Hint
for...in loops over keys of an object.
β Incorrect
The for...in loop iterates over the keys of the object, so concatenating keys 'a', 'b', and 'c' results in 'abc'.
β Predict Output
intermediate2: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);
Attempts:
2 left
π‘ Hint
Object.entries returns key-value pairs as arrays.
β Incorrect
Object.entries returns [['x',10], ['y',20]]. The loop sums values 10 + 20 = 30.
β Predict Output
advanced2: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);
Attempts:
2 left
π‘ Hint
Object.keys returns keys, map transforms values.
β Incorrect
Object.keys returns ['a','b','c']. Mapping each key to obj[key]*2 gives [2,4,6].
β Predict Output
advanced2: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);
Attempts:
2 left
π‘ Hint
Object.values returns values only.
β Incorrect
Object.values returns [5,10]. The loop concatenates '5-' and '10-' resulting in '5-10-'.
β Predict Output
expert2: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);
Attempts:
2 left
π‘ Hint
Object.values returns nested objects, destructuring extracts x.
β Incorrect
Object.values(data) returns [{x:1}, {x:2}]. Destructuring {x} extracts values 1 and 2, summed to 3.