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
Group statements in a block
easy10 minPut two statements inside an if block and show the result.
- 2
Use a standalone block
easy10 minA bare { } block is valid — use one and show "blocked".
- 3
Scope a variable to a block
easy10 minDeclare inside a block and read it there.
- 4
Read an outer variable inside a block
easy10 minInner blocks can read outer values.
- 5
Shadow a name in a block
medium10 minAn inner block can declare its own version of a name.
Find the bug(5)
- 1
Fix the missing braces
easy8 minOnly the first line belongs to the if without braces.
- 2
Fix the variable read outside its block
easy8 minA const declared in a block is not visible outside it.
- 3
Fix the unclosed block
easy8 minA missing closing brace breaks the whole script.
- 4
Fix the shadowed name
easy8 minThe inner declaration hides the value that should be shown.
- 5
Fix the block attached to nothing
easy8 minThe 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