A simple tip calculator web application that demonstrates basic math operations, DOM manipulation, event handling, and input validation in JavaScript.
- Bill Amount Input: Enter the total bill amount
- Tip Percentage Selection: Choose from preset percentages (10%, 15%, 20%, 25%) or enter a custom percentage
- Split Bill: Divide the total among multiple people
- Real-time Calculations: Automatically calculates tip amount and total
- Input Validation: Ensures numeric inputs are valid
- Animations: Smooth transitions when updating results
- Input field for bill amount
- Buttons for common tip percentages
- Custom tip percentage input
- Number of people input for bill splitting
- Display areas for tip amount, total amount, and per-person cost
- Modern, responsive design with gradient backgrounds
- Smooth animations and transitions
- Visual feedback for active buttons and input validation
let billAmount = 0;
let tipPercentage = 20; // Default to 20%
let numPeople = 1;The validateNumericInput() function checks if inputs are valid numbers within acceptable ranges:
- Bill amount must be >= 0
- Custom tip percentage must be >= 0
- Number of people must be >= 1
- Preset buttons update the
tipPercentagevariable and highlight the active button - Custom tip input overrides preset selection when entered
The calculateTip() function performs the core math:
- Validates bill amount input
- Calculates tip amount:
tipAmount = (billAmount * tipPercentage) / 100 - Calculates total:
totalAmount = billAmount + tipAmount - If splitting, calculates per-person cost:
amountPerPerson = totalAmount / numPeople
- Results are updated with
animateResult()function for smooth visual feedback - Split bill section shows/hides based on number of people
- Tip buttons: Update tip percentage and visual state
- Custom tip input: Override button selection
- Calculate button: Trigger calculations
- Input fields: Real-time validation
- Open
index.htmlin a web browser - Enter the bill amount
- Select a tip percentage or enter a custom one
- Optionally, enter number of people to split the bill
- Click "Calculate" to see results
- Basic Math Operations: Multiplication and division for tip calculations
- DOM Manipulation: Updating text content and element visibility
- Event Listeners: Handling user interactions (clicks, input changes)
- Input Validation: Ensuring user inputs are valid before calculations
- CSS Animations: Smooth transitions and visual feedback
- Responsive Design: Works on different screen sizes
Works in all modern browsers that support ES6 features.