Copy a number, change the copy, and report the original.
Acceptance criteria
- 1.#out should show 1.
JavaScript Tutorial · JavaScript Type System
These 10 tasks go with the JavaScript Primitives and References chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “Primitives are copied by value and objects are copied by reference.” .
1.Copy a primitiveexercise · easy · 10 min
Copy a number, change the copy, and report the original.
Passes when: #out should show 1.
2.Share an objectexercise · easy · 10 min · practice plan
Assign an object to a second name, change it, and report the first.
Passes when: #out should show 2.
3.Copy an object properlyexercise · easy · 10 min · practice plan
Spread the object so the original is left alone.
Passes when: #out should show 1 and 2.
4.Copy an arrayexercise · easy · 10 min · practice plan
Spread an array, push to the copy, and report both lengths.
Passes when: #out should show 2 and 3.
5.Change an object in a functionexercise · medium · 10 min · practice plan
Show that a property changed inside a function is visible outside.
Passes when: #out should show Grace.
6.Fix the object shared instead of copiedbug fix · easy · 8 min
A plain assignment shares the object.
Passes when: #out should show 1 and 2.
7.Fix the array sharedbug fix · easy · 8 min · practice plan
Pushing to the second name changes the first array too.
Passes when: #out should show 2 and 3.
8.Fix the objects compared by contentsbug fix · easy · 8 min · practice plan
Two separate objects are never equal, however alike.
Passes when: #out should show different.
9.Fix the reassignment inside a functionbug fix · easy · 8 min · practice plan
Replacing the whole object inside a function is not visible outside.
Passes when: #out should show Grace.
10.Fix the nested object still sharedbug fix · easy · 8 min · practice plan
Spreading copies only the top level, so the inner object is shared.
Passes when: #out should show London.
Between them these tasks cover Primitives, References and Copying.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.