Drill

ProblemsJavaScript › reporting

Which month grew the most

mediumreportingJavaScript

A board slide calls out the month with the largest jump in revenue over the month before it.

biggestJump(points: list<Point>) → string?

Solve it in the editor →

Where you start

function biggestJump(points) {
  
}

Worked examples

CallResult
biggestJump([{"month":"jan","total":100},{"month":"feb","total":150},{"month":"mar","total":160}])"feb"
biggestJump([{"month":"jan","total":100},{"month":"feb","total":110},{"month":"mar","total":120}])"feb"
biggestJump([{"month":"jan","total":100},{"month":"feb","total":90}])null
biggestJump([{"month":"jan","total":100}])null

Hint

Start from index 1 and keep the best strictly-greater difference, so ties keep the first one.

Reference solution in JavaScript
function biggestJump(points) {
  let bestMonth = null, bestJump = 0;
  for (let i = 1; i < points.length; i++) {
    const jump = points[i].total - points[i - 1].total;
    if (jump > bestJump) { bestJump = jump; bestMonth = points[i].month; }
  }
  return bestMonth;
}

The same problem in another language

More reporting problems in JavaScript