JavaScript Primitives and References — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Primitives and References 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
Copy a primitive
easy10 minCopy a number, change the copy, and report the original.
- 2
Share an object
easy10 minAssign an object to a second name, change it, and report the first.
- 3
Copy an object properly
easy10 minSpread the object so the original is left alone.
- 4
Copy an array
easy10 minSpread an array, push to the copy, and report both lengths.
- 5
Change an object in a function
medium10 minShow that a property changed inside a function is visible outside.
Find the bug(5)
- 1
Fix the object shared instead of copied
easy8 minA plain assignment shares the object.
- 2
Fix the array shared
easy8 minPushing to the second name changes the first array too.
- 3
Fix the objects compared by contents
easy8 minTwo separate objects are never equal, however alike.
- 4
Fix the reassignment inside a function
easy8 minReplacing the whole object inside a function is not visible outside.
- 5
Fix the nested object still shared
easy8 minSpreading copies only the top level, so the inner object is shared.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace