BACK
PracticeInterview: reduce() cart price * qty
PRACTICE · 67 · 練

Interview: reduce() cart price * qty

Compute the cart total, taking quantity into account.

  1. Solve the task in the editor — no long theory.
  2. Run the check and compare your output with the expected result.
  3. Stuck? Open the solution and carry the approach into your own code.
Solution spoiler · click to reveal
const { from, reduce } = Rx;

const lines = [
  { price: 10, qty: 2 },
  { price: 5, qty: 1 },
];

const result$ = from(lines).pipe(
  reduce((sum, line) => sum + line.price * line.qty, 0)
);

result$.subscribe(value => console.log(value));
script.ts // TypeScript
CONSOLE · Console output
Hit Run to see the result...