In this lesson you will
- Plan a small tool as inputs, rules and outputs before writing code
- Read messy input safely, including Arabic digits and commas
- Calculate money in whole fils and split a total fairly
- Show results in English or Arabic with Intl.NumberFormat and test the finished tool
Step 1: plan inputs, rules and outputs
Before any code, write three short lists. Inputs: the bill amount, the tip as a percentage, the number of people and the language. Rules: the bill must be a number above 0 and at most 100,000; people must be a whole number from 1 to 50; a tip of 0 is allowed. Outputs: the tip, the new total and the amount per person, plus one clear message when an input is wrong. These lists become your functions and your tests, and they stop you from discovering half-way through that you never decided what happens with 0 people.
Then write the HTML from the HTML course: a <form> with a <label> around every field, inputmode="decimal" so phones show a number keyboard with a decimal point, a <select> for the tip so nobody types "10%" by hand, and an <output> with aria-live="polite" so screen readers announce new results. Add novalidate to the form, because your own checks will give better, bilingual messages than the browser’s. Restaurants in the Gulf often add a service charge already, so a "No tip" choice is a real option, not an afterthought.
Console
- Console messages, errors, form data and clicked links appear here.
Type in the boxes and change the selects. Object.fromEntries(new FormData(form)) turns the whole form into one object of strings, which is exactly what the next step has to clean up.
Sign in free to read the full lesson
You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.
Unlocks after sign-in:
- 4 more sections with live code examples
- Common mistakes to avoid
- Hands-on coding exercise with automatic checks
- Practice quiz and the graded lesson test
Free forever. No card, no paywall.
Questions people ask
How do I convert Arabic digits to English numbers in JavaScript?
Replace each digit from ٠to ٩ with its position in the string "٠١٢٣٤٥٦٧٨٩", using replace with a regular expression and a function, and turn the Arabic decimal separator into a dot. Do the same for Persian digits if your visitors use them, then call Number on the result.
How do I format a number as dirhams or riyals in JavaScript?
Create new Intl.NumberFormat(locale, options) with the options style: "currency" and currency: "AED", then call .format(amount). Use "SAR" for riyals, "QAR", "KWD", "BHD" or "OMR" for the other Gulf currencies; Kuwaiti, Bahraini and Omani amounts get three decimals automatically.
Should I add a tip when the bill already has a service charge?
That is a personal choice, and the calculator should not decide it. Read the bill first: if it shows a service charge, many people choose a small tip or none, which is why "No tip" is the first option in this project.
What should I learn after this JavaScript course?
Build two or three tools of your own first, then pick one direction. TypeScript adds types that catch many bugs before they run. A framework such as React helps with larger interfaces. Node.js lets you write servers and APIs in the same language.
Where can I publish a small JavaScript tool for free?
A tool made of HTML, CSS and JavaScript files is a static site, so free static hosts such as GitHub Pages, Cloudflare Pages and Netlify can serve it. Read each host’s current free plan limits before you rely on it.
Sources
Facts in this lesson were checked against these official pages on 15 November 2026.
- MDN: Intl.NumberFormat
- MDN: Number.isNaN()
- MDN: Math.round()
- MDN: Remainder (%)
- MDN: The output element
- MDN: inputmode
- Unicode CLDR: number symbols and numbering systems
- ISO 4217 currency codes
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.