JavaScript instanceof — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript instanceof 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
Check an instance
easy10 minReport whether the object was built from the class.
- 2
Follow inheritance
easy10 minReport whether a dog is also an animal.
- 3
Show it is not symmetric
easy10 minReport whether an animal is a dog.
- 4
Read the exact class
easy10 minReport the exact class name of the instance.
- 5
Check behaviour instead
medium10 minCall the method only if the object actually has one.
Find the bug(5)
- 1
Fix typeof used to identify a class
easy8 mintypeof only ever says object for an instance.
- 2
Fix the operands the wrong way round
easy8 minThe object goes on the left and the class on the right.
- 3
Fix the parent checked as a child
easy8 minA parent instance is not an instance of the child class.
- 4
Fix instanceof used for the exact class
easy8 mininstanceof is true for parents too, so it cannot tell the exact class.
- 5
Fix the class compared as a string
easy8 minconstructor.name is a string, but the class itself is not.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace