Work out a bill with a service charge

“Café bill”, a White belt challenge from the CurlyScript Dojo. It uses what Grade 3: Operators: Math, Comparisons & Logic teaches, and 3 tests check your answer.

The challenge

Write bill(price, quantity) that returns the total cost plus a 10% service charge, rounded to 2 decimal places as a number.

Example
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.

What the tests check

  • bill(40, 2) gives 88
  • bill(12.5, 3) gives 41.25
  • bill(9.99, 1) gives 10.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
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.

Practice makes it stick

CurlyScript is a free game that teaches JavaScript from zero: short lessons, an explanation after every answer, and Dojo challenges like this one.

Start learning