Course progress0%
Course content

Module 1

Module 2

Module 3

Module 4

Module 5

Module 6

Module 7

Module 8

Module 9

Module 10

Module 11

Module 12

Module 13

Module 14

Module 15

Module 16

Module 17

Module 18

45 min

Operators, Expressions and the Price Calculator

Translate an agreed order of calculations into expressions

By the end of this lesson

  • Translate an agreed order of calculations into expressions
  • Recognize conversion and rounding decisions

Clarify the calculation

An order has a subtotal of 100 units, a 10% discount, then a 20% tax on the discounted amount. Predict the final price. Does the stated order matter?

For this exercise: discount 10, discounted amount 90, tax 18, total 108. Charging tax on the original subtotal would implement a different rule. These are fictional exercise policies, not tax advice.

Operators express decisions

+, -, * and / perform arithmetic on numbers. % gives a remainder. Multiplication and division bind more tightly than addition and subtraction; parentheses state grouping explicitly. <, <=, > and >= compare amounts. Use === and !== for equality and inequality without implicit conversion.

const subtotal = 100;
const discountRate = 0.10;
const taxRate = 0.20;
const discountedSubtotal = subtotal * (1 - discountRate);
const tax = discountedSubtotal * taxRate;
const total = discountedSubtotal + tax;
console.assert(total === 108);
console.log(`Total: ${total} units`);

A template literal uses backticks; ${total} inserts a value into the text. It is useful for display, not for storing a numeric amount.

A conversion trap

console.assert("20" + 5 === "205");
console.assert(Number("20") + 5 === 25);
console.assert(Number.isNaN(Number("unknown")));

With text, + can concatenate rather than add. Number requests conversion. A failed numeric conversion can produce NaN, meaning not-a-number. Use Number.isNaN, not equality with NaN. Conversion is not validation: an empty string converts to 0, which may be inappropriate for a missing price.

JavaScript numbers use floating-point representation. Some decimal fractions cannot be represented exactly. For these labs use whole units or integer minor units and state a rounding rule before introducing fractional charges. Do not claim every decimal calculation is exact.

Independent lab

Use a subtotal of 200, a 25% discount and 10% tax on the discounted amount. List the intermediate expected amounts, implement them with named constants and display a readable quote. Then change the policy: tax is zero. Which step changes?

Correction

The discounted amount is 150, tax 15 and total 165. With zero tax, total is 150. Changing only taxRate is sufficient for this formula. Test both cases. Do not remove the named tax step merely because one example has a zero rate.

Review

Submit the policy in words, the program and both predictions. A correct number without a clear rule is insufficient: another person should be able to explain why the operations occur in that order.

Lesson complete?

Your progress is saved on this device.