Level 2: Intermediate JavaScript Exercises
Level 2 is the JavaScript you write every day on a real website: filtering a
user list, totalling a cart, merging settings, reading API JSON, cleaning up
tags and checking form input. Each lesson also names the one trap that tends
to cause bugs, like sort() comparing numbers as text.
Start from any of the four starting points — map & filter, Destructuring, JSON & try/catch or Regular Expressions. The Shopping Cart project at the end combines reduce, spread and immutable updates.
The 11 exercises in this level
- 1 map & filter New arrays from old ones Keep the items you want with filter and reshape each one with map, without ever touching the original array. Start here — no prerequisites
- 2 reduce Many values into one Fold a whole array into a single result, such as a cart total, by carrying a running value from item to item. Recommended first: map & filter
- 3 Destructuring Unpack objects and arrays Pull the fields you need straight out of objects and arrays, give them fallback values and unpack them right in a function's parameters. Start here — no prerequisites
- 4 Spread & Rest Copy and merge with ... Use the three dots to copy arrays and objects, merge user settings over defaults and collect leftover fields. Recommended first: Destructuring
- 5 Optional Chaining Safe paths with ?. and ?? Read deeply nested API data without crashing on a missing field, and supply a fallback when the value isn't there. Recommended first: Destructuring
- 6 Sorting with Compare Functions Numbers, objects and the text trap Order numbers and product lists correctly by handing sort a compare function, and sort a copy so the caller's array stays as it was. Recommended first: map & filter
- 7 JSON & try/catch Read API text safely Turn objects into JSON text and back, and keep your page running when a server sends something that isn't valid JSON. Start here — no prerequisites
- 8 Map & Set Unique values and counters Drop duplicates with a Set and keep counts in a Map, then turn the results back into ordinary arrays. Recommended first: reduce
- 9 Regular Expressions Check the shape of text Validate form input such as colour codes with a short pattern, using character classes, counts and anchors. Start here — no prerequisites
- 10 Words: split & join Cut text into pieces and glue it back Break a string into an array of words, rework each word and stitch them into a variable name or slug. Recommended first: Regular Expressions
- 11 Shopping Cart Immutable cart helpers Build the two helpers behind every online shop's basket: add a product without mutating the cart, and compute the amount to pay. Recommended first: reduce, Spread & Rest
Frequently asked questions
- Do I need to finish JavaScript Level 1 first?
- No, every level is open. If you can already write a function, use arrays and objects and build strings with template literals, you can start here.
- Why do the tasks insist on not changing the input?
- Frameworks like React spot changes by comparing old and new values. If you edit an array in place, the old and new values are the same object and the screen may not update, so returning a new copy is a habit worth building.
- Are these features supported in every browser?
- Yes. Spread, destructuring, optional chaining, ?? and Map and Set work in all current browsers and in Node.js, so you can use them in real projects today.
- What if my answer differs from the expected one?
- The tests only check what your function returns, not how you wrote it. A for loop and a reduce call both pass, as long as the result is right.