Skip to main content

JavaScript Map Methods — exercises and bug fixes

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

    easy10 min

    Spread the keys into an array and join them.

  2. 2

    Total the values

    easy10 min

    Add up every value in the Map.

  3. 3

    Loop with forEach

    easy10 min

    Build a line using the value and the key.

  4. 4

    Convert to an object

    easy10 min

    Turn the Map into a plain object and read a property.

  5. 5

    Convert from an object

    medium10 min

    Build a Map from an object and read a value.

Find the bug(5)

  1. 1

    Fix the forEach arguments swapped

    easy8 min

    In a Map forEach the value comes first, then the key.

  2. 2

    Fix map called on the iterator

    easy8 min

    keys returns an iterator, which has no array methods.

  3. 3

    Fix keys totalled instead of values

    easy8 min

    The numbers are the values, not the keys.

  4. 4

    Fix the Map spread directly into an object

    easy8 min

    Object.fromEntries is what converts the pairs.

  5. 5

    Fix the object passed straight to Map

    easy8 min

    A Map needs pairs, which Object.entries produces.

Ready to write the code?

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

Open the workspace