Drill

ProblemsTypeScript › reporting

Running balance down a statement

easyreportingTypeScript

An account statement shows the balance after every movement, not just the final figure.

runningTotal(amounts: list<int>) → list<int>

Solve it in the editor →

Where you start

function runningTotal(amounts: number[]): number[] {
  
}

Worked examples

CallResult
runningTotal([1,2,3])[1,3,6]
runningTotal([5,-5,5])[5,0,5]
runningTotal([7])[7]
runningTotal([])[]

Hint

Carry one accumulator down the list and push it after each step.

Reference solution in TypeScript
function runningTotal(amounts: number[]): number[] {
  let sum = 0;
  const result: number[] = [];
  for (const a of amounts) {
    sum += a;
    result.push(sum);
  }
  return result;
}

The same problem in another language

More reporting problems in TypeScript