JavaScript for in Loop — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript for in Loop lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.
Exercises(5)
- 1
List the property names
easy10 minJoin the property names of the object with a dash.
- 2
Count the properties
easy10 minCount how many properties the object has.
- 3
Add up the values
easy10 minAdd every value in the object together and show the total.
- 4
Find a property by its value
easy10 minFind which subject scored 25 and show its name.
- 5
Join names and values
medium10 minBuild a string of every property as name=value, separated by commas.
Find the bug(5)
- 1
Fix the value used as a name
easy8 minThe loop variable holds the property name, not the value.
- 2
Fix the missing brackets
easy8 minAdding the property name instead of the value gives the wrong total.
- 3
Fix the dot used with a variable
easy8 minscores.key looks for a property actually named "key".
- 4
Fix the counter that counts letters
easy8 minThis counts the characters of each name instead of the properties.
- 5
Fix the missing break
easy8 minWithout break the loop keeps going and reports the last match.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace