All JavaScript projects

JavaScript project

Order Bill Calculator

Calculate a customer's order bill with a membership discount and delivery charge.

Step 1: Practice variables, data types, operators, and conditions.

  • Variables
  • Data types
  • Operators
  • Conditions

What you will build

  • Enter a customer name, order subtotal, and membership status.
  • Apply a 10% discount for members when the original subtotal is at least Rs 1,000.
  • Add Rs 40 delivery, or free delivery when the subtotal after discount is at least Rs 500.
  • Show the discount, delivery charge, and final total. These are example rules for a small shop; taxes are not included.

How to run this project

  1. Create a file named index.html in a new folder.
  2. Copy the complete code below into the file and save it.
  3. Open index.html in your browser. No installation or API key is needed.

Complete project code

This single file includes the HTML interface, basic CSS styling, and JavaScript behavior. The script runs after the HTML so all the elements are available.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Order Bill Calculator</title>
  <style>
    body { font-family: system-ui, sans-serif; max-width: 640px; margin: 40px auto; padding: 0 20px; line-height: 1.6; }
    form { display: grid; gap: 12px; }
    input, select, button { font: inherit; padding: 8px; }
    button { cursor: pointer; }
    button:disabled { cursor: default; }
    li { margin: 12px 0; overflow-wrap: anywhere; }
    li button { margin-left: 12px; }
    input[type="checkbox"] { margin-right: 8px; }
    #answers { display: grid; gap: 8px; }
  </style>
</head>
<body>
<h1>Order Bill Calculator</h1>
<p>Members save 10% on orders of Rs 1,000 or more.
Delivery is free from Rs 500 after discount; otherwise it costs Rs 40.</p>
<form id="bill-form">
  <label for="customer-name">Customer name</label>
  <input id="customer-name" maxlength="60" required>
  <label for="subtotal">Order subtotal (Rs)</label>
  <input id="subtotal" type="number" min="0.01" max="100000" step="0.01" required>
  <label><input id="is-member" type="checkbox"> Customer is a member</label>
  <button type="submit">Calculate bill</button>
</form>
<p id="message" role="status"></p>
<section id="bill" hidden>
  <h2>Bill summary</h2>
  <p id="customer-result"></p>
  <p id="discount-result"></p>
  <p id="delivery-result"></p>
  <p id="total-result"></p>
</section>
<script>
// 1. Connect the form and result area to JavaScript.
const form = document.querySelector("#bill-form");
const nameInput = document.querySelector("#customer-name");
const subtotalInput = document.querySelector("#subtotal");
const memberInput = document.querySelector("#is-member");
const message = document.querySelector("#message");
const bill = document.querySelector("#bill");

form.addEventListener("submit", (event) => {
  event.preventDefault();
  bill.hidden = true;

  // 2. Read three data types: string, number, and boolean.
  const customerName = nameInput.value.trim();
  const subtotal = subtotalInput.valueAsNumber;
  const isMember = memberInput.checked;

  if (customerName === "") {
    message.textContent = "Enter a customer name.";
    return;
  }

  if (!Number.isFinite(subtotal) || subtotal < 0.01 || subtotal > 100000) {
    message.textContent = "Enter an amount from Rs 0.01 to Rs 100,000.";
    return;
  }

  // 3. Calculate in whole paise: 100 paise = 1 rupee.
  const subtotalPaise = Math.round(subtotal * 100);
  let discountPaise = 0;

  if (isMember && subtotalPaise >= 100000) {
    discountPaise = Math.round(subtotalPaise * 0.10);
  }

  const discountedPaise = subtotalPaise - discountPaise;
  let deliveryPaise = 4000;

  if (discountedPaise >= 50000) {
    deliveryPaise = 0;
  }

  const totalPaise = discountedPaise + deliveryPaise;

  // 4. Convert back to rupees only when displaying the bill.
  document.querySelector("#customer-result").textContent = "Customer: " + customerName;
  document.querySelector("#discount-result").textContent = "Discount: Rs " + (discountPaise / 100).toFixed(2);
  document.querySelector("#delivery-result").textContent = "Delivery: Rs " + (deliveryPaise / 100).toFixed(2);
  document.querySelector("#total-result").textContent = "Total: Rs " + (totalPaise / 100).toFixed(2);
  bill.hidden = false;
  message.textContent = "Bill calculated for " + customerName + ".";
});
</script>
</body>
</html>

How the code works

  1. 1. Read a name, amount, and membership flag

    customerName is a string, subtotal is a number, and isMember is a boolean (true or false). trim removes extra spaces from the name. valueAsNumber reads the numeric input directly; checked reads the checkbox state. const is used for values that are not reassigned during this calculation.

    JavaScript
    const customerName = nameInput.value.trim();
    const subtotal = subtotalInput.valueAsNumber;
    const isMember = memberInput.checked;
  2. 2. Reject invalid input with conditions

    if runs a block only when its condition is true. === checks whether the name is empty. The amount check uses ! (not) and || (or) to reject a non-finite number or an amount outside the allowed range. return stops the handler after displaying an error.

  3. 3. Apply the member discount

    Multiplying rupees by 100 and rounding produces whole paise. let allows discountPaise to change from its initial value of 0. The && operator requires both conditions: the customer must be a member and the original subtotal must be at least 100,000 paise (Rs 1,000). The discount is rounded to the nearest paise.

    JavaScript
    let discountPaise = 0;
    
    if (isMember && subtotalPaise >= 100000) {
      discountPaise = Math.round(subtotalPaise * 0.10);
    }
  4. 4. Calculate delivery and the final bill

    Subtraction removes the discount. A second condition changes deliveryPaise from 4,000 to 0 when the discounted amount reaches Rs 500. Addition combines the discounted amount with delivery. A member ordering Rs 1,200 receives Rs 120 off, free delivery, and a final bill of Rs 1,080.

  5. 5. Display the result after submitting

    The small submit callback is the browser wiring for this project; the business rules use variables and conditions without custom functions or arrays. preventDefault keeps the page open. textContent writes safe plain text into each result. Dividing by 100 converts paise to rupees, and toFixed(2) returns a display string with two decimal places. The old bill is hidden before validation so an invalid entry never leaves a stale result visible.

Try it yourself

For a member, enter Rs 1,200: discount Rs 120, delivery Rs 0, total Rs 1,080. For a non-member, Rs 450 costs Rs 490 with delivery. Rs 500 qualifies for free delivery. At Rs 1,000 a member pays Rs 900, while a non-member pays Rs 1,000. Try a name containing only spaces to see the validation message.

Back to all projects
Digital AI Academy

DevBrainBox Portal is proudly developed and managed by

Digital AI Academy
About the academy