Exercises
This chapter introduces no new concepts.
💡 Why this matters: Arrays, objects, destructuring, spread, rest, optional chaining, and iteration all click into place through practice, not through reading. These exercises walk through every idea from this module using concrete starting data, so you can build each answer step by step and check it against a clear expected result.
Exercises
-
Given
const groceries = ["milk", "eggs", "bread"], log the item at index2and the array’s.length. Expected:breadand3. -
Starting from
const line = ["Erin", "Sam"],.push()"Priya"onto the end, then.pop()the last person off. Log the array after each step. Expected after.push():[ 'Erin', 'Sam', 'Priya' ]. Expected after.pop():[ 'Erin', 'Sam' ]. -
Given
const stock = ["apple", "banana", "cherry", "mango"], use.includes()to check whether"cherry"is present, and.indexOf()to find the position of"mango". Expected:trueand3. -
Given
const nums = [5, 10, 15, 20, 25], use.slice()to pull out just[10, 15, 20]without changingnums. -
Given
const prices = [8, 12, 20], use.map()to build a new array where each price is doubled. Expected:[ 16, 24, 40 ]. -
Given
const ages = [10, 16, 19, 22, 14], use.filter()to keep only the ages 18 and over. Expected:[ 19, 22 ]. -
Given
const points = [7, 3, 9, 5], use.reduce()to sum them into a single total, starting the accumulator at0. Expected:24. Before checking the answer, trace the accumulator by hand after each step. -
Given
const product = { title: "Notebook", price: 4.5 }, readtitlewith dot notation andpricewith bracket notation. Then add a new propertyinStockset totrue, and log the full object. Expected final object:{ title: 'Notebook', price: 4.5, inStock: true }. -
Given
const car = { make: "Toyota", model: "Corolla", year: 2022 }, log the results ofObject.keys(car),Object.values(car), andObject.entries(car). Expected:[ 'make', 'model', 'year' ], then[ 'Toyota', 'Corolla', 2022 ], then[ [ 'make', 'Toyota' ], [ 'model', 'Corolla' ], [ 'year', 2022 ] ]. -
Given
const dimensions = [1920, 1080], destructure it intowidthandheight, then destructureconst [zoom = 100] = []to see the default value100kick in when the array is empty. Expected:1920 1080, then100. -
Given
const account = { username: "sam_dev", role: "editor" }, destructure it intohandle(renamed fromusername) andpermissions(renamed fromrole, with a default of"viewer"in case it’s missing). Expected:sam_dev editor. -
Given
const base = { volume: 5, muted: false }andconst custom = { volume: 8 }, use spread to build a merged object wherecustom’s values win. Expected:{ volume: 8, muted: false }. -
Given
const scores = [100, 90, 80, 70], use rest destructuring to split it intobest(the first score) andothers(an array of the rest). Expected:100and[ 90, 80, 70 ]. -
Given
const trip = { destination: "Accra", traveler: { name: "Jordan Reyes" } }, first try loggingtrip.traveler.passport.numberdirectly and read theTypeErrorit throws. Then rewrite it astrip.traveler.passport?.number ?? "Not on file"and confirm it now logsNot on fileinstead of crashing. -
Given
const attendees = ["Maya", "Priya", "Erin"]andconst rsvp = { Maya: true, Priya: false, Erin: true }, loop overattendeeswithfor...ofto log each name, then loop overrsvpwithfor...into log each name alongside theirtrue/falsevalue. Finally, rewrite theattendeesloop using.forEach()instead offor...of.
Recap
- These exercises covered every tool from this module: arrays and their methods, objects and object methods, destructuring, spread, rest, nested objects, optional chaining, nullish coalescing, and the three ways to iterate a collection.
- If any exercise felt shaky, especially
.reduce(),?., or thefor...ofversusfor...inchoice, revisit that lesson before moving on. Those three are the ideas most worth being solid on before the rest of the course builds on top of them.
Next up: Module 6, Strings and Regular Expressions.