Drill

ProblemsJavaScript › finance

Average monthly figures for a year

mediumfinanceJavaScript

Twelve monthly totals summarise a year. Report the average monthly figure, dropping the fractional minor unit.

yearlyAverage(monthlyTotals: list<int>) → int

Solve it in the editor →

Where you start

function yearlyAverage(monthlyTotals) {
  
}

Worked examples

CallResult
yearlyAverage([1,1,1,1,1,1,1,1,1,1,1,1])1
yearlyAverage([1,2,3,4,5,6,7,8,9,10,11,12])6
yearlyAverage([0,0,0,0,0,0,0,0,0,0,0,0])0
yearlyAverage([100,100,100,100,100,100,100,100,100,100,100,100])100

Hint

Guard the length, sum the entries, divide by twelve.

Reference solution in JavaScript
function yearlyAverage(monthlyTotals) {
  if (monthlyTotals.length < 12) return 0;
  let sum = 0;
  for (const v of monthlyTotals) sum += v;
  return Math.floor(sum / 12);
}

The same problem in another language

More finance problems in JavaScript