CodingNic

Arrays, Objects & Iteration

Exercises

Arrays, Objects & Iteration 30 min read

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

  1. Given const groceries = ["milk", "eggs", "bread"], log the item at index 2 and the array’s .length. Expected: bread and 3.

  2. 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' ].

  3. Given const stock = ["apple", "banana", "cherry", "mango"], use .includes() to check whether "cherry" is present, and .indexOf() to find the position of "mango". Expected: true and 3.

  4. Given const nums = [5, 10, 15, 20, 25], use .slice() to pull out just [10, 15, 20] without changing nums.

  5. Given const prices = [8, 12, 20], use .map() to build a new array where each price is doubled. Expected: [ 16, 24, 40 ].

  6. Given const ages = [10, 16, 19, 22, 14], use .filter() to keep only the ages 18 and over. Expected: [ 19, 22 ].

  7. Given const points = [7, 3, 9, 5], use .reduce() to sum them into a single total, starting the accumulator at 0. Expected: 24. Before checking the answer, trace the accumulator by hand after each step.

  8. Given const product = { title: "Notebook", price: 4.5 }, read title with dot notation and price with bracket notation. Then add a new property inStock set to true, and log the full object. Expected final object: { title: 'Notebook', price: 4.5, inStock: true }.

  9. Given const car = { make: "Toyota", model: "Corolla", year: 2022 }, log the results of Object.keys(car), Object.values(car), and Object.entries(car). Expected: [ 'make', 'model', 'year' ], then [ 'Toyota', 'Corolla', 2022 ], then [ [ 'make', 'Toyota' ], [ 'model', 'Corolla' ], [ 'year', 2022 ] ].

  10. Given const dimensions = [1920, 1080], destructure it into width and height, then destructure const [zoom = 100] = [] to see the default value 100 kick in when the array is empty. Expected: 1920 1080, then 100.

  11. Given const account = { username: "sam_dev", role: "editor" }, destructure it into handle (renamed from username) and permissions (renamed from role, with a default of "viewer" in case it’s missing). Expected: sam_dev editor.

  12. Given const base = { volume: 5, muted: false } and const custom = { volume: 8 }, use spread to build a merged object where custom’s values win. Expected: { volume: 8, muted: false }.

  13. Given const scores = [100, 90, 80, 70], use rest destructuring to split it into best (the first score) and others (an array of the rest). Expected: 100 and [ 90, 80, 70 ].

  14. Given const trip = { destination: "Accra", traveler: { name: "Jordan Reyes" } }, first try logging trip.traveler.passport.number directly and read the TypeError it throws. Then rewrite it as trip.traveler.passport?.number ?? "Not on file" and confirm it now logs Not on file instead of crashing.

  15. Given const attendees = ["Maya", "Priya", "Erin"] and const rsvp = { Maya: true, Priya: false, Erin: true }, loop over attendees with for...of to log each name, then loop over rsvp with for...in to log each name alongside their true/false value. Finally, rewrite the attendees loop using .forEach() instead of for...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 the for...of versus for...in choice, 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.