Level 3 · Programming Skills · Course 3.1 JavaScript Programming

Module 22: Loops and Algorithms

Goal: plan the steps a program takes before writing it, and use loops and array methods to transform, search, sort and total data, with a feel for which approaches stay fast as data grows.

You'll need: your level-3 folder and catalogue.js from Module 21.

Lessons

  1. 1What an algorithm is15–25 minutes✓
  2. 2Transforming data with map, filter and reduce15–25 minutes✓
  3. 3Searching15–25 minutes✓
  4. 4Sorting15–25 minutes✓
  5. 5Efficiency15–25 minutes✓

🛠️Module practice: Project A, part 2, a catalogue report

Extend catalogue.js to print a report:

  1. Total number of products and total stock value (with reduce).
  2. Products that need reordering (stock below 5), sorted by stock.
  3. The number of products in each category (hint: reduce into an object, or a loop).
  4. A searchProducts(query) function that ignores case and spaces.
  5. The three most expensive products, without changing the original array.

Write the pseudocode for each part first, as comments. Commit.

✅Module check

Explain in a few sentences, in your own words:

What's an algorithm, and why might code that works perfectly with 100 items become unusable with 100,000?

Show a good answer

A good answer mentions: an algorithm is a precise set of steps from input to output, planned in pseudocode before coding; map, filter, reduce, search and sort are common building blocks. Code with a loop inside a loop (for example find inside map over large lists) grows with the square of the data: fine for 100 items, but billions of steps for 100,000. Building a Map or Set for lookups keeps it fast.

📝Module quiz

10 questions · 10 marks each · 100 marks in all · pass mark 70% · retry as often as you like ·

1. What three things should pseudocode make clear?

Why each option is right or wrong

  • “The programming language, the editor and the version” (Incorrect): Pseudocode is deliberately language-free.
  • “The variable names, the types and the comments” (Incorrect): Those are coding details; pseudocode comes before them.
  • “The author, the date and the file name” (Incorrect): That's documentation, not the algorithm.
  • “The input, the output, and the steps between them” is correct.

    Pseudocode describes an algorithm in plain words before writing code. Knowing what goes in, what should come out and the steps in between lets you check the logic before any syntax gets in the way.

    • Example: input: cart items; output: total; steps: add each price × quantity, then add GST.

📖 Revise: Lesson 1: What an algorithm is

Question ID module-22-q1 · Report a mistake in this question
2. You want prices with 18% GST added. Which method?

Why each option is right or wrong

  • “reduce” (Incorrect): reduce produces a single value, like a total, not a list of new prices.
  • “filter” (Incorrect): filter keeps or drops items; it doesn't change them.
  • “forEach” (Incorrect): forEach runs code for each item but returns nothing, so you'd have to build the new list yourself.
  • “map” is correct.

    map transforms every item and returns a new array of the same length: prices.map(p => p * 1.18).

    • filter keeps some items; reduce combines them into one value; find returns one item.

📖 Revise: Lesson 2: Transforming data with map, filter and reduce

Question ID module-22-q2 · Report a mistake in this question
3. What's the 0 for in reduce((sum, p) => sum + p.price, 0)?

Why each option is right or wrong

  • “It skips the first item in the list” (Incorrect): Nothing is skipped; 0 is the starting total.
  • “It sets the index where reduce begins” (Incorrect): reduce begins at the first item; 0 is the initial value.
  • “It limits the result so it can't go below zero” (Incorrect): It doesn't limit anything; it's just the starting value.
  • “It's the starting value of sum” is correct.

    reduce carries a running result from item to item. The second argument sets where it starts. Without it, reduce uses the first item as the start, which here would be an object, not a number, and on an empty list it throws an error.

    • Safe default for totals: start at 0.

📖 Revise: Lesson 2: Transforming data with map, filter and reduce

Question ID module-22-q3 · Report a mistake in this question
4. A search for "pen" misses "Pen Stand". What's the likely bug?

Why each option is right or wrong

  • “The array wasn't sorted before searching” (Incorrect): A simple search checks every item; order doesn't matter.
  • “includes only matches whole words” (Incorrect): includes matches any part of the text; case is the problem.
  • “The list is too long to search fully” (Incorrect): Long lists are searched fully; it would just take longer.
  • “The comparison is case-sensitive” is correct.

    'pen' and 'Pen' are different strings to a computer. Lower-case both sides before comparing: name.toLowerCase().includes(term.toLowerCase()).

    • Also consider: extra spaces, so trim the search term.

📖 Revise: Lesson 3: Searching

Question ID module-22-q4 · Report a mistake in this question
5. Why can binary search only be used on sorted data?

Why each option is right or wrong

  • “Sorted data takes less memory” (Incorrect): Sorting doesn't shrink data.
  • “It reads the list from the end backwards” (Incorrect): It starts from the middle, not the end.
  • “Unsorted data can't be stored in an array” (Incorrect): Arrays hold data in any order.
  • “It decides which half the target is in, which needs the data in order” is correct.

    Binary search looks at the middle item. If the target is smaller, it must be in the left half; if larger, the right half. That logic only holds when the data is sorted.

    • Speed: about 20 checks for a million items, instead of up to a million.

📖 Revise: Lesson 3: Searching

Question ID module-22-q5 · Report a mistake in this question
6. [5, 25, 100].sort() returns [100, 25, 5]. Why?

Why each option is right or wrong

  • “sort reverses lists that are already in order” (Incorrect): sort doesn't reverse; it compared the values as text.
  • “Numbers can only be sorted from largest to smallest” (Incorrect): Numbers can be sorted either way with a compare function.
  • “sort only looks at the number of digits” (Incorrect): It compares characters, not digit counts.
  • “Without a compare function, items are compared as text” is correct.

    The default sort turns items into strings and compares them character by character. '1' comes before '2', which comes before '5', so '100' < '25' < '5'.

    • Numbers: list.sort((a, b) => a - b).

📖 Revise: Lesson 4: Sorting

Question ID module-22-q6 · Report a mistake in this question
7. For each of 10,000 orders, code searches 10,000 customers. Roughly how many steps?

Why each option is right or wrong

  • “About 20,000” (Incorrect): That's what you'd get with a Map lookup; nested loops multiply instead.
  • “About 10,000” (Incorrect): That counts only the outer loop.
  • “About 1 million” (Incorrect): Off by a factor of 100: it's 10,000 times 10,000.
  • “Up to 100 million” is correct.

    A loop inside a loop multiplies: 10,000 × 10,000 = 100,000,000 comparisons in the worst case. That's why nested searches get slow as data grows.

    • Fix: build a Map of customers by ID once, then each lookup is near-instant: about 20,000 steps total.

📖 Revise: Lesson 5: Efficiency

Question ID module-22-q7 · Report a mistake in this question
8. Should you aim for the fastest possible code every time?

Why each option is right or wrong

  • “Yes, because users always notice small delays” (Incorrect): Users don't notice microseconds; they notice slow pages, which you find by measuring.
  • “Yes, because optimising later is impossible” (Incorrect): Optimising later is normal and easier with clear code.
  • “No, because speed doesn't matter in modern apps” (Incorrect): Speed matters when it's slow enough for people to notice.
  • “No: write clear code first, and optimise when something is measurably slow” is correct.

    Most code is fast enough. Clever optimisations make code harder to read and change, so they're worth it only where measurement shows a real problem.

    • Measure first: time it with real data.
    • Then fix the hot spot, not everything.

📖 Revise: Lesson 5: Efficiency

Question ID module-22-q8 · Report a mistake in this question
9. Why write the steps before asking AI to code something?

Why each option is right or wrong

  • “AI tools can't write code without pseudocode” (Incorrect): They can, but the result is more likely to miss what you meant.
  • “Pseudocode makes the final code run faster” (Incorrect): Pseudocode doesn't affect speed.
  • “It's required before code can be committed” (Incorrect): There's no such rule; it's a good habit.
  • “You can describe exactly what you want and check its code against it” is correct.

    Clear steps give the AI a precise brief, and give you a checklist to compare against the code it returns. Without them, it's hard to spot when the code solves a different problem.

    • Bonus: writing the steps often reveals gaps in your own thinking.

📖 Revise: Lesson 1: What an algorithm is

Question ID module-22-q9 · Report a mistake in this question
10. How do you sort without changing the original array?

Why each option is right or wrong

  • “Use filter before sorting” (Incorrect): filter makes a copy, but only as a side effect, and it's an odd way to say 'copy'.
  • “Use sort twice, which restores the order” (Incorrect): The second sort doesn't restore anything.
  • “Nothing needed: sort returns a new array” (Incorrect): sort changes the original and returns that same array.
  • “Use toSorted, or sort a copy: [...list].sort(...)” is correct.

    sort changes the array it's called on. toSorted (newer) returns a sorted copy, and sorting a spread copy does the same in any browser.

    • Why care: other code may rely on the original order.

📖 Revise: Lesson 4: Sorting

Question ID module-22-q10 · Report a mistake in this question

ProLearners Assist