Skip to main content

JavaScript Default Parameters — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Default Parameters 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 a default

    easy10 min

    Give a greeting function a default name of friend and call it with no argument.

  2. 2

    Override the default

    easy10 min

    Call the same greeting function with the name Ada.

  3. 3

    A default quantity

    easy10 min

    Give quantity a default of 1 and show the total for a price of 50.

  4. 4

    Build a default from another parameter

    easy10 min

    Make height default to width so one argument gives a square. Show it for 6.

  5. 5

    A default empty array

    medium10 min

    Default the list to an empty array so counting never fails. Show the count with no argument.

Find the bug(5)

  1. 1

    Fix the missing default

    easy8 min

    With no default the name becomes undefined.

  2. 2

    Fix the colon used instead of equals

    easy8 min

    A default is written with an equals sign.

  3. 3

    Fix the default that is never used

    easy8 min

    Passing null does not trigger the default.

  4. 4

    Fix the default in the wrong place

    easy8 min

    The default is on the first parameter, so the value lands wrongly.

  5. 5

    Fix the default that crashes

    easy8 min

    Without a default the length of undefined cannot be read.

Ready to write the code?

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

Open the workspace