JavaScript object exercises
Objects fail in JavaScript by handing back undefined: a property that is not there, a nested value one level too deep, a counter that was never initialised and is now NaN. These exercises are short functions with one such bug each.
Find where the undefined first appears, fix it, and run the tests.
The objects bugs you will meet
- obj[key]++ on a missing key gives NaN; start it at 0.
- user.address.city crashes when address is missing; use ?. or check first.
- Assigning an object does not copy it; both names share one object.
- typeof null is "object", so a type check can let null through.
- Object keys are always strings, so obj[1] and obj["1"] are the same key.
The exercises
Easy
- getName gives back undefinedAsync and race conditions
- Word counter shows NaNNull and undefined
- Counting settings returns undefinedNull and undefined
- Null profile object crashes name lookup with TypeErrorNull and undefined
- Array indexOf looking up object returns negative oneLogic
- Deduplicating objects in array with Set returns duplicatesLogic
Medium
Hard
- Cached loader runs 3 times instead of onceAsync and race conditions
- Counter method crashes inside forEachScope and closure
- New tracker starts with the old tracker's totalScope and closure
- Search freezes when the value is missingInfinite loops
- Type checker says null is an objectType
- City lookup still crashes with ?.Null and undefined
Errors you might be searching for
More JavaScript practice by topic
Questions
How hard are these?
Mostly medium and hard. If you are just starting, the JavaScript array exercises are a gentler place to begin.
Do they run in the browser?
Yes, in a sandboxed Web Worker. Nothing to install.