Problems › TypeScript › reporting
Which month grew the most
A board slide calls out the month with the largest jump in revenue over the month before it.
- Compare each month with the one directly before it, in the order given.
- Only increases count; a month that fell is not a jump.
- On a tie, the earlier month wins.
- Fewer than two months, or no increase anywhere, gives null.
biggestJump(points: list<Point>) → string?
Where you start
function biggestJump(points: Point[]): string | null {
}
Worked examples
| Call | Result |
|---|---|
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 TypeScript
function biggestJump(points: Point[]): string | null {
let bestMonth: string | null = null;
let 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;
}