Skip to content
loading...

re: Daily Challenge #137 - Help the Bookseller VIEW POST

FULL DISCUSSION
 

JavaScript, with map ( for constant lookups )

const l = ["ABART 20", "CDXEF 50", "BKWRK 25", "BTSQZ 89", "DRTYM 60"];
const m = ["A", "B", "C", "W"];

const store = l.reduce((acc, inventory) => {
  const [[code], qty] = inventory.split(" ");
  const stock = (acc[code] || 0) + Number(qty);
  return {
    ...acc,
    [code]: stock
  }
}, {});

const counts = m.reduce((result, key) => ({
  ...result,
  [key]: store[key] || 0
}), {})

const print = obj => Object.entries(obj)
    .map(([k, v]) => `(${k} : ${v})`)
    .join(" - ");

console.log(print(counts));
code of conduct - report abuse