Skip to main content

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. 1

    List the property names

    easy10 min

    Join the property names of the object with a dash.

  2. 2

    Count the properties

    easy10 min

    Count how many properties the object has.

  3. 3

    Add up the values

    easy10 min

    Add every value in the object together and show the total.

  4. 4

    Find a property by its value

    easy10 min

    Find which subject scored 25 and show its name.

  5. 5

    Join names and values

    medium10 min

    Build a string of every property as name=value, separated by commas.

Find the bug(5)

  1. 1

    Fix the value used as a name

    easy8 min

    The loop variable holds the property name, not the value.

  2. 2

    Fix the missing brackets

    easy8 min

    Adding the property name instead of the value gives the wrong total.

  3. 3

    Fix the dot used with a variable

    easy8 min

    scores.key looks for a property actually named "key".

  4. 4

    Fix the counter that counts letters

    easy8 min

    This counts the characters of each name instead of the properties.

  5. 5

    Fix the missing break

    easy8 min

    Without 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