Problems › JavaScript › finance
How much survives a round trip
Money changes from currency A to B to C, and the rates are given in basis points per leg. Find what is left in the end.
- Each leg multiplies by its basis-point rate and divides by 10000.
- The whole trip is combined into one integer division: amount × a × b ÷ (10000 × 10000).
- The numbers stay small enough that no 32-bit value overflows.
exchangeRoundTrip(amountMinor: int, rateABbps: int, rateBCbps: int) → int
Where you start
function exchangeRoundTrip(amountMinor, rateABbps, rateBCbps) {
}
Worked examples
| Call | Result |
|---|---|
exchangeRoundTrip(200, 1000, 2500) | 5 |
exchangeRoundTrip(100, 5000, 2000) | 10 |
exchangeRoundTrip(1000, 1000, 1000) | 10 |
exchangeRoundTrip(100, 8000, 1000) | 8 |
Hint
Multiply all three factors, then divide once by a hundred million.
Reference solution in JavaScript
function exchangeRoundTrip(amountMinor, rateABbps, rateBCbps) {
return Math.floor((amountMinor * rateABbps * rateBCbps) / (10000 * 10000));
}