Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to loop over the keys of the object.
Javascript
const obj = {a: 1, b: 2, c: 3};
for (const [1] in obj) {
console.log([1]);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using 'value' instead of 'key' in the for...in loop variable.
Trying to use 'for...of' directly on an object.
β Incorrect
The 'for...in' loop iterates over the keys of the object, so the variable should be named 'key' to represent each key.
2fill in blank
mediumComplete the code to get the values of the object using Object.values().
Javascript
const obj = {x: 10, y: 20};
const values = Object.[1](obj);
console.log(values); Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using Object.keys() instead of Object.values().
Trying to call a non-existent method like Object.get().
β Incorrect
Object.values(obj) returns an array of the object's values.
3fill in blank
hardFix the error in the code to correctly iterate over object entries.
Javascript
const obj = {a: 1, b: 2};
for (const [key, [1]] of Object.entries(obj)) {
console.log(key, [1]);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using 'item' or 'index' which do not represent the value.
Trying to destructure incorrectly.
β Incorrect
Object.entries(obj) returns [key, value] pairs, so the second variable should be 'value'.
4fill in blank
hardFill both blanks to create a new object with keys in uppercase and values doubled.
Javascript
const obj = {a: 1, b: 2};
const newObj = Object.fromEntries(
Object.entries(obj).map(([[1], [2]]) => [[1].toUpperCase(), [2] * 2])
);
console.log(newObj); Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Swapping key and value in destructuring.
Using unclear variable names like 'item' or 'entry'.
β Incorrect
When destructuring entries, the first element is the key and the second is the value.
5fill in blank
hardFill all three blanks to filter object entries with values greater than 10 and create a new object.
Javascript
const obj = {a: 5, b: 15, c: 25};
const filteredObj = Object.fromEntries(
Object.entries(obj)
.filter(([[1], [2]]) => [2] [3] 10)
);
console.log(filteredObj); Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using '<' instead of '>' in the filter condition.
Swapping key and value in destructuring.
β Incorrect
Destructure entries as [key, value], then filter where value > 10.