JavaScript Objects and Arrays Quiz: reduce and sort Practice
Test your JavaScript objects and arrays with 12 questions and three programs on JSON, shallow vs deep copy, sort, map, filter, reduce and destructuring.
- Course: JavaScript study plan
- Module: Objects, arrays and destructuring
- Kind: Checkpoint — cleared at 70%
- Reading time: 24 min
- Runtime: Node 16
Checkpoint — Objects, arrays and destructuring is the checkpoint that closes the Objects, arrays and destructuring module: a graded quiz and whole-program exercises, passed at 70%.
Instructions
This checkpoint covers object literals and their shorthands, property access and testing, key iteration order and JSON, references versus shallow and deep copies with Object.freeze and immutable updates, arrays — construction, length, the mutating core, sort with a comparator — the method toolbox with reduce patterns, and destructuring and spread in all their forms.
How it works. Twelve questions and three programs; 70% on the questions and every program accepted clears the module.
Before you start, make sure you can answer:
- When bracket access is required, and what
Object.hasOwnadds overin. - What
JSON.stringifydrops and whatJSON.parsecannot bring back. - Shallow versus deep copy; what
Object.freezedoes not freeze; how to update a nested field immutably. - Why
new Array(3)is not[0, 0, 0], and what[10, 9, 1].sort()returns. mapversusforEach,findversusfilter, and whyreduceneeds a seed.- Object versus array destructuring, defaults on
undefined, and which spread wins on duplicate keys.
The programs are a grouping-and-summary report built from reduce and Object.entries, an immutable-update engine that applies dotted-path changes to a JSON document without mutating the original, and a records processor using destructuring in every parameter list.
Common questions
Why is new Array(3) not the same as [0, 0, 0]?
new Array(3) creates an array of length 3 with three holes and no elements, which map and forEach skip. Array(3).fill(0) or Array.from({ length: 3 }, () => 0) gives three real zeros.
What does [10, 9, 1].sort() return in JavaScript?
It returns [1, 10, 9], because the default sort compares elements as strings. Pass a numeric comparator, (x, y) => x - y, to get [1, 9, 10], and remember that sort also reorders the original array.
What does JSON.parse not bring back?
JSON.parse returns plain objects and arrays, so whatever JSON.stringify dropped or converted stays lost: methods, undefined values, symbols, and Date objects, which come back as ISO strings rather than Dates.
Exercises
Sales summary by region
Each line is region product qty price (integers). Group the sales by region with reduce, then for every region in alphabetical order print <region>: revenue=<sum of qty*price> items=<sum of qty> top=<product with the largest revenue in that region> (ties broken by product name ascending). Finish with regions=<count>.
Example:
north pen 10 2
south pad 3 5
north pad 2 5
north ink 4 3
south pen 1 2
→
north: revenue=42 items=16 top=pen
south: revenue=17 items=4 top=pad
regions=2Immutable path updates
The first line is a JSON document; every following line is set <dotted.path> <JSON value>. Implement setPath(obj, keys, value) returning a new object with the value placed at the path — spreading each object on the path, creating intermediate objects when missing, and sharing every untouched subtree. Apply the commands in order, then print final=<JSON>, original=<JSON> (which must be unchanged) and shared=<top-level keys whose value is still the same reference in both, joined by ",", or - if none>.
Example:
{"name":"cfg","db":{"host":"x","port":1},"tags":["a"]}
set db.port 5432
set name "prod"
→
final={"name":"prod","db":{"host":"x","port":5432},"tags":["a"]}
original={"name":"cfg","db":{"host":"x","port":1},"tags":["a"]}
shared=tagsRecords, destructured everywhere
Each line is name, age, city. Using destructuring in every callback parameter list — ({ city, name }), ([a], [b]), { age } — print the people grouped by city (cities alphabetical, names within a city sorted), one line each as <city>: <names space-separated>; then oldest=<name of the oldest; first one on ties> average=<mean age, 1 decimal>; then first=<first person's name> others=<how many follow> via const [{ name: first }, ...others] = people.
Example:
Ada, 36, London
Bob, 41, Paris
Cy, 29, London
Dee, 41, Rome
→
London: Ada Cy
Paris: Bob
Rome: Dee
oldest=Bob average=36.8
first=Ada others=3In this module: Objects, arrays and destructuring
- Object literals — properties, keys, iteration and JSON
- References, shallow and deep copies, and freezing
- Arrays — dynamic, sparse, and sorted lexically by default
- The array methods toolbox — map, filter, reduce and friends
- Destructuring and spread — taking things apart and putting them together
- Checkpoint — Objects, arrays and destructuring (this lesson)
← Destructuring and spread — taking things apart and putting them together · The prototype chain — how JavaScript objects inherit →