Skip to content
Codewilds

Lesson 24 of 34 · Objects

JavaScript Objects Boss: The Ledger Lich

Boss battlehard100 XP2 min read

Ledgerlich, Keeper of Debts blocks the way. Every test you pass deals damage — pass them all to defeat it and claim a loot chest.

Ledgerlich, the undead accountant of the Merchant Guild, has cursed the books: every transaction is still there, but nobody can make sense of them anymore. Balance the ledger and the curse breaks.

Battle plan

The ledger is an array of transaction objects. Each one records which hero bought which item, how many and at what price:

const transactions = [
  { hero: "Ada", item: "potion", qty: 2, price: 5 },
  { hero: "Linus", item: "sword", qty: 1, price: 40 },
  { hero: "Ada", item: "elixir", qty: 1, price: 15 },
];

A transaction's cost is qty * price. To win, combine the whole chapter:

  • Destructuring makes every loop and callback readable: ({ hero, qty, price }) => ....
  • An object as a lookup table adds up spending per hero.
  • Object.entries turns those totals into pairs you can compare.
  • Set counts and collects distinct values.
  • JSON.stringify produces the final report.

Don't modify the transactions array or its objects. Ledgerlich audits everything.

Assignment

Write four functions:

  1. spendingByHero(transactions) returns a plain object mapping each hero's name to the total they spent. For an empty ledger, return {}.
  2. topSpender(transactions) returns the name of the hero who spent the most in total (not the one with the single biggest purchase). If two heroes are tied, return the one who appears first in the ledger. For an empty ledger, return null.
  3. itemsBoughtBy(transactions, hero) returns the distinct items that hero bought, sorted alphabetically. If the hero bought nothing, return [].
  4. ledgerSummary(transactions) returns a JSON string of an object with three numbers: total (everything spent by everyone), heroes (number of distinct heroes) and items (number of distinct items).

With the ledger used in the tests:

const transactions = [
  { hero: "Ada", item: "potion", qty: 2, price: 5 },
  { hero: "Linus", item: "sword", qty: 1, price: 40 },
  { hero: "Ada", item: "elixir", qty: 1, price: 15 },
  { hero: "Grace", item: "map", qty: 1, price: 12 },
  { hero: "Ada", item: "potion", qty: 1, price: 5 },
  { hero: "Ada", item: "shield", qty: 1, price: 25 },
];

spendingByHero(transactions); // { Ada: 55, Linus: 40, Grace: 12 }
topSpender(transactions);     // "Ada"
itemsBoughtBy(transactions, "Ada"); // ["elixir", "potion", "shield"]
ledgerSummary(transactions);  // '{"total":107,"heroes":3,"items":5}'

Premium lesson

Unlock the full Learn JavaScript course

  • All 34 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 8 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.

Keep learning

Need a quick reminder? The JavaScript cheat sheet has every topic of Learn JavaScript on one page.

Lesson last updated on .

Write code ↓
JavaScript+100 XP

Lesson 24 is part of the full course. The first 8 lessons are free.

Premium lesson

Unlock the full Learn JavaScript course

  • All 34 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 8 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.