Problems › JavaScript › pricing
Week-on-week price change
A pricing dashboard shows how far each item moved since last week, as a percentage.
- Rounded to one decimal place.
- A previous price of zero has no meaningful percentage: return 0.
- A drop is negative.
percentChange(before: int, after: int) → float
Where you start
function percentChange(before, after) {
}
Worked examples
| Call | Result |
|---|---|
percentChange(100, 125) | 25 |
percentChange(200, 150) | -25 |
percentChange(300, 310) | 3.3 |
percentChange(100, 100) | 0 |
Hint
difference / before * 100, then round to one decimal by multiplying by 10, rounding, and dividing back.
Reference solution in JavaScript
function percentChange(before, after) {
if (before === 0) return 0;
const pct = ((after - before) * 100) / before;
return Math.round(pct * 10) / 10;
}