JavaScript, inside out · Chapter 4 of 51
Numbers and Math
By the end of this chapter you can do sums with money and quantities: whole shares and what is left over, rounding, numbers read out of text, and amounts that come out to the exact paisa. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: First day at Tapri
- 5 tasks
- JavaScript
- In the all labs plan
The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.
Why you need this
Tapri has started a wallet. Customers top it up through UPI, and on the first day a student who topped up ₹0.10 and then ₹0.20 saw a balance of ₹0.30000000000000004. Meera also needs a bill split so that friends pay whole rupees each, Ravi needs the cups read out of whatever a customer types in the quantity box (which happily accepts "2.5"), and Anjali wants every price on the receipt with two decimals.
You can already add numbers with +. But dividing a bill gives a long decimal, adding two wallet amounts can leave a tail of digits, and what a customer types arrives as text, which is not a number at all.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
splitBill: whole-rupee shares and what is left
Write splitBill(total, people). total is a bill in whole rupees and people is how many share it (at least 1). Return an object with two keys: each, the whole number of rupees every person pays (the share rounded down), and left, the rupees left over that nobody's share covers.
splitBill(125, 3) gives { each: 41, left: 2 }. When the bill splits evenly, left is 0: splitBill(120, 3) gives { each: 40, left: 0 }.
- Task 2
parseCups: the number of cups from typed text
Write parseCups(text). Read the whole number at the start of what the customer typed and return it as a number. Spaces before the number are fine, and reading stops at the first character that is not a digit: '3 cups' gives 3, ' 4 ' gives 4, '2.5' gives 2 and '12kulhads' gives 12.
When the text does not start with a number, return 0: 'two' gives 0, and so does empty text ''.
- Task 3 · interview question
Interview: addMoney, 0.1 + 0.2 that comes out as 0.3
Write addMoney(a, b). Add two wallet amounts in rupees and round the sum to the nearest paisa (two decimal places). Return a number, not text.
addMoney(0.1, 0.2) gives 0.3, addMoney(19.99, 0.01) gives 20, addMoney(1.1, 2.2) gives 3.3, and whole amounts stay whole: addMoney(40, 25) gives 65.
- Task 4
formatPrice: a price with two decimals
Write formatPrice(amount). Return text: the rupee sign ₹ followed by the amount rounded to exactly two decimal places, with no space between them.
formatPrice(42.5) gives '₹42.50', formatPrice(40) gives '₹40.00', formatPrice(12.345678) gives '₹12.35', and formatPrice(0.1 + 0.2) gives '₹0.30'.
- Task 5
isValidQty: whole numbers from 1 to 20
Write isValidQty(qty). Return true only when qty is a whole number, of the number type, from 1 to 20 (1 and 20 included). Return false for everything else.
0, 21 and -3 give false, and so do 2.5, NaN and the text '2'.
What you will learn
- Arithmetic: /, % (the remainder) and ** (power).
- The Math toolbox: rounding down, up and to the nearest whole number, and the largest or smallest of a few numbers.
- Text into numbers: Number, parseInt and parseFloat, and how to spot NaN.
- Decimals and money: why 0.2 + 0.4 is not 0.6, and toFixed.
More in First day at Tapri
Build "Numbers and Math" yourself
Tapri, a chai brand going online, needs its menu, orders and kitchen in code. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.