Skip to main content

JavaScript Code Blocks — exercises and bug fixes

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

    Group statements in a block

    easy10 min

    Put two statements inside an if block and show the result.

  2. 2

    Use a standalone block

    easy10 min

    A bare { } block is valid — use one and show "blocked".

  3. 3

    Scope a variable to a block

    easy10 min

    Declare inside a block and read it there.

  4. 4

    Read an outer variable inside a block

    easy10 min

    Inner blocks can read outer values.

  5. 5

    Shadow a name in a block

    medium10 min

    An inner block can declare its own version of a name.

Find the bug(5)

  1. 1

    Fix the missing braces

    easy8 min

    Only the first line belongs to the if without braces.

  2. 2

    Fix the variable read outside its block

    easy8 min

    A const declared in a block is not visible outside it.

  3. 3

    Fix the unclosed block

    easy8 min

    A missing closing brace breaks the whole script.

  4. 4

    Fix the shadowed name

    easy8 min

    The inner declaration hides the value that should be shown.

  5. 5

    Fix the block attached to nothing

    easy8 min

    The condition is separated from its block by a semicolon.

Ready to write the code?

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

Open the workspace