Looping
JavaScript · Unit 7: Objects
- Full plan
- JavaScript
- 10 exercises
Object.keys(obj) gives you an array with the names of the properties, and Object.values(obj), one with their values.
Since they're arrays, you already know how to loop over them.
const marks = { math: 9, art: 7 };
console.log(Object.keys(marks));
console.log(Object.values(marks));Prints
[ 'math', 'art' ] [ 9, 7 ]
The rest of the explanation is in the lesson, which is part of the full plan.
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Predict the output
What does this code print?
2. Complete the code
Complete it to add up the month's bills.
3. Find the bug
This program should print how many different items there are, "We have 2 items". Which line has the error?
4. Predict the output
What does this code print?
5. Multiple choice
What does this code do?
6. Predict the output
What does this code print?
7. Complete the code
Complete it to count how many T-shirts there are of each size.
8. Put the lines in order
Order the function that adds up all the month's bills with
reduce.9. Find the case that fails
summary(marks)gets an object with the mark for each subject and should return a string like"Average: 8", or"No marks"if there are no subjects. Which call makes it fail?10. Predict the output
You count the words in a sentence. What does it print?
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.