Skip to main content

JavaScript for of Loop — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript for of 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

    Add up an array

    easy10 min

    Add every number in the array together and show the total.

  2. 2

    Join the values

    easy10 min

    Join the fruit names together with a comma.

  3. 3

    Count the letters

    easy10 min

    Count how many characters are in the text, ignoring spaces.

  4. 4

    Use the index with entries

    easy10 min

    Show each fruit with its index, joined by commas.

  5. 5

    Find the largest number

    medium10 min

    Find the largest number in the array and show it.

Find the bug(5)

  1. 1

    Fix for in used instead of for of

    easy8 min

    for in gives the index, so the totals are wrong.

  2. 2

    Fix the index treated as a value

    easy8 min

    This joins the indexes instead of the fruit names.

  3. 3

    Fix the missing entries call

    easy8 min

    Without entries there is no index to unpack.

  4. 4

    Fix the comparison that never updates

    easy8 min

    The comparison is the wrong way round, so the smallest wins.

  5. 5

    Fix the break that should be continue

    easy8 min

    The loop stops at the first space instead of skipping it.

Ready to write the code?

The editor, the automatic checks and the worked solutions open up with an account.

Open the workspace