Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to get an array of key-value pairs from the object.
Javascript
const obj = {a: 1, b: 2};
const entries = Object.[1](obj);
console.log(entries); Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using Object.keys instead of Object.entries
Forgetting to call the method with parentheses
β Incorrect
Use Object.entries(obj) to get an array of key-value pairs.
2fill in blank
mediumComplete the code to loop over the entries of the object.
Javascript
const obj = {x: 10, y: 20};
for (const [key, value] of Object.[1](obj)) {
console.log(key, value);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using Object.keys which returns only keys
Using Object.values which returns only values
β Incorrect
Use Object.entries(obj) to loop over key-value pairs.
3fill in blank
hardFix the error in the code to correctly get entries from the object.
Javascript
const obj = {name: 'Alice', age: 25};
const entries = Object.[1](obj);
console.log(entries); Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Misspelling the method name
Using singular form 'entry'
β Incorrect
The correct method name is entries. Typos cause errors.
4fill in blank
hardFill both blanks to create an object from entries and log it.
Javascript
const entries = [['a', 1], ['b', 2]]; const obj = Object.[1](entries); console.[2](obj);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using Object.entries instead of fromEntries
Using console.print which is not a function
β Incorrect
Use Object.fromEntries to create an object from entries and console.log to print it.
5fill in blank
hardFill all three blanks to filter entries with values greater than 10 and create a new object.
Javascript
const obj = {a: 5, b: 15, c: 25};
const filteredEntries = Object.entries(obj).filter(([[1], [2]]) => [3] > 10);
const newObj = Object.fromEntries(filteredEntries);
console.log(newObj); Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using wrong variable names in destructuring
Comparing key instead of value
β Incorrect
Use key and value as variable names in destructuring, then filter by value > 10.