The challenge
Write bill(price, quantity) that returns the total cost plus a 10% service charge, rounded to 2 decimal places as a number.
bill(40, 2) // 88
bill(12.5, 3) // 41.25
Try it
Write your answer below, then press Check my answer. Your code runs right here in your browser, and the tests tell you what works.
It prints
What the tests check
bill(40, 2)gives88bill(12.5, 3)gives41.25bill(9.99, 1)gives10.99
Hints
Stuck? Open one hint at a time. Try again after each one.
Hint 1
The subtotal is price * quantity. Adding 10% is the same as multiplying by 1.1.
Hint 2
Math.round(x * 100) / 100 rounds to 2 decimal places and keeps it a number.
A solution
There’s always more than one right way. Read it, make sure you understand it, then try writing it yourself without looking.
Show one possible solution
function bill(price, quantity) {
const total = price * quantity * 1.1;
return Math.round(total * 100) / 100;
}
Learn what you need
This exercise uses what Grade 3: Operators: Math, Comparisons & Logic teaches. Read those notes first if any of it is new.