Invoice discount in cents
For each invoice amount in whole cents, apply a 10% discount only when the amount is at least 1000 cents. The discount is rounded down to a whole cent and capped at 250 cents. Print the final payable amount for each invoice, preserving input order. Avoid floating-point money calculations.
Input
First line N; then N non-negative integer invoice amounts in cents.
Output
One payable amount in cents per line.
Example 1
Input
4 999 1000 2500 5000
Output
999 900 2250 4750
999 is below the threshold; 1000 pays 900; 2500 gets a 250-cent discount; 5000 is still capped at 250.
Constraints
- 0 <= N <= 200 invoices.
- Amounts are non-negative integer cents small enough for safe integer arithmetic.
Hints
Hint 1 of 2
Check the inclusive 1000-cent threshold before calculating any discount.
Hint 2 of 2
Use integer division with Math.floor, then cap at 250.
Solution
Show a reference solution and explanation
function payable(cents) {
if (cents < 1000) return cents;
const discount = Math.min(250, Math.floor(cents / 10));
return cents - discount;
}
const n = Number(readline());
for (let i = 0; i < n; i++) console.log(payable(Number(readline())));
Why it works
Amounts stay in integer cents from input to output. Below the threshold there is no discount. At or above it, floor division computes ten percent without fractional cents and Math.min enforces the cap. The boundary tests distinguish 999 from 1000.
Lesson for this exercise: JavaScript Functions: Declarations, Arrows and Return Values