Skip to main content

JavaScript Class Fields — exercises and bug fixes

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

    Declare a field

    easy10 min

    Give the class a count field starting at zero and increase it once.

  2. 2

    Give each instance its own array

    easy10 min

    Push to one basket and report both lengths.

  3. 3

    Use a field in the constructor

    easy10 min

    Build a message from a field and a parameter.

  4. 4

    Override a field

    easy10 min

    Let the constructor replace the field default when a value is given.

  5. 5

    Use an arrow field

    medium10 min

    Make greet an arrow field so it keeps this when detached.

Find the bug(5)

  1. 1

    Fix the field name that does not match

    easy8 min

    The method increases a name that was never declared as a field.

  2. 2

    Fix the shared array

    easy8 min

    A static field is shared, so both baskets see the same items.

  3. 3

    Fix the missing field declaration

    easy8 min

    The class declares no field, so the property does not exist.

  4. 4

    Fix the normal method losing this

    easy8 min

    A detached method needs an arrow field to keep its instance.

  5. 5

    Fix the default wiped by the constructor

    easy8 min

    The constructor assigns even when no value was passed, losing the field's default.

Ready to write the code?

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

Open the workspace