‹ back to the board

Saturday, October 3

Novicesyntax & basics

The Reagent Roll of the Cinderpike Expedition

The Guild of Ashen Alchemists is outfitting an expedition to the fire-vents of Cinderpike. Before the mules are loaded, the Guild's quartermaster must know how many measures of the required reagents the Guild's store actually holds.

The store's inventory is a dictionary, stock, mapping each reagent's name (a string) to the number of measures on hand (a non-negative integer). The expedition's requirements are a list of reagent names, wanted.

Write tallyReagents(stock, wanted), which returns an integer: the sum, over every name in wanted, of that name's count in stock.

Rules: - A name in wanted that is not a key in stock contributes 0. - Names match exactly, including upper and lower case. - If a name appears more than once in wanted, its count is added once for each appearance. - If wanted is empty, return 0.

Neither argument should be modified.

  • dictionary and object access
  • Novice

examples

  1. case 1stock of { "ember salt": 4, "bog myrtle": 2, "moonwort": 7 }, wanted of ["moonwort", "ember salt"]11
  2. case 2stock of { "ember salt": 4, "moonwort": 7 }, wanted of ["ghost pepper", "moonwort"]7 (the store has no ghost pepper, so it adds 0)
  3. case 3stock of { "bog myrtle": 2 }, wanted of ["bog myrtle", "bog myrtle"]4 (listed twice, counted twice)
reward
140 XP
signature
tallyReagents
not yet run

TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.

loading the editor…
  • sums two stocked reagents
  • missing reagent counts as zero
  • empty wanted list
  • empty stock
  • duplicate names counted each time
  • reagent with zero measures
  • names are case sensitive
  • all wanted names missing
The Reagent Roll of the Cinderpike Expedition · bitwick