Skip to main content

JavaScript throw — exercises and bug fixes

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

    Throw on bad input

    easy10 min

    Refuse a negative age and report the message.

  2. 2

    Stop the function

    easy10 min

    Show that nothing after the throw runs.

  3. 3

    Guard a division

    easy10 min

    Throw when dividing by zero and report the message.

  4. 4

    Return rather than throw

    easy10 min

    A missing user is normal, so return null instead.

  5. 5

    Rethrow with context

    medium10 min

    Catch an error and throw a clearer one.

Find the bug(5)

  1. 1

    Fix the string thrown

    easy8 min

    A thrown string has no message property.

  2. 2

    Fix the missing new

    easy8 min

    Error must be created with new to be thrown properly.

  3. 3

    Fix the value returned instead of thrown

    easy8 min

    Returning a message lets the caller ignore the problem.

  4. 4

    Fix the throw for a normal outcome

    easy8 min

    A missing user is expected, so it should not throw.

  5. 5

    Fix the missing validation

    easy8 min

    Without a check the text is coerced and a wrong answer comes back.

Ready to write the code?

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

Open the workspace