Cafe bill total
A cafe till exports three order lines. Each line contains a decimal price followed by an integer quantity. Calculate the combined bill and print it with exactly two digits after the decimal point.
Input
Three lines, each containing a price and quantity separated by one space.
Output
One line: the bill total formatted to two decimal places.
Example 1
Input
3.50 2 2.25 1 1.00 3
Output
12.25
7.00 + 2.25 + 3.00 gives 12.25.
Constraints
- 0.01 <= price <= 1000
- 1 <= quantity <= 100
Hints
Hint 1 of 3
Map Number over the split input fields.
Hint 2 of 3
Add price * quantity inside the loop.
Hint 3 of 3
toFixed(2) supplies the required two decimal places.
Solution
Show a reference solution and explanation
let total = 0;
for (let i = 0; i < 3; i++) {
const [price, quantity] = readline().split(' ').map(Number);
total += price * quantity;
}
console.log(total.toFixed(2));
Why it works
Each input line becomes two numbers through destructuring. The loop accumulates their product, and toFixed(2) turns the final number into the exact display format requested by the till.
Lesson for this exercise: JavaScript Operators: Arithmetic, Comparison and Logic