Operators and Expressions

3.One Character That Cost Money

A

In this chapter

We'll learn how code calculates and compares — expressions and operators: arithmetic, comparison, logical and assignment — and find the one character behind BUG-142.

12–14 min

The Problem in Real Life

Deeper in the pricing code, Anna finds the line that decides the discount:

if (count > GROUP_SIZE) { price = price * 0.9; }

She reads it out loud, slowly. "If the count is greater than five, give ten percent off." Then she stops. "Greater than five. So six or more. But the website promises five or more."

Table — Testing the old rule: count > 5
TicketsWebsite promiseCode gives
4No discountNo discount ✓
510% offNo discount ✗
610% off10% off ✓
This whole line is a row — one record
A

Five isn't greater than five. That's the whole bug — one missing character.

Anna

"Greater Than" vs. "Greater Than or Equal To"

One character, real money

> and >= differ by one symbol — and by every group of exactly five customers.

Edges are where bugs live

The code is right for 4 and 6 tickets. It's wrong only at exactly 5 — the edge of the rule.

Expressions and Operators

An expression is any piece of code that produces a value. 2 + 3 is an expression (it produces 5). count > 5 is an expression (it produces true or false). Even a single variable like price is an expression. Programs are built from expressions joined together.

An operator is the symbol that does the work in an expression — like + or >. There are four groups you'll use constantly:

  • Arithmetic operators: do maths. + add, - subtract, * multiply, / divide, and % (called modulo), which gives the remainder after dividing: 7 % 3 is 1. Modulo is handy for questions like "is this number even?" (n % 2 === 0).
  • Comparison operators: compare two values and always produce a boolean (true or false). > greater than, < less than, >= greater than or equal to, <= less than or equal to, === equal to, !== not equal to.
  • Logical operators: combine booleans. && (AND) is true only if both sides are true. || (OR) is true if at least one side is true. ! (NOT) flips true to false and false to true.
  • Assignment operators: store values. = puts a value into a variable. Shortcuts combine a calculation with assignment: count += 1 means count = count + 1; price *= 0.9 means price = price * 0.9.
Table — The four groups of operators
GroupOperatorsExampleResult
Arithmetic+ - * / %10 % 31
Comparison> < >= <= === !==5 >= 5true
Logical&& || !true && falsefalse
Assignment= += -= *=count += 1count goes up by 1
Table — Logical operators: the truth table
ABA && BA || B!A
truetruetruetruefalse
truefalsefalsetruefalse
falsetruefalsetruetrue
falsefalsefalsefalsetrue
Table — After the fix: count >= 5
TicketsWebsite promiseCode gives
4No discountNo discount ✓
510% off10% off ✓
610% off10% off ✓
This whole line is a row — one record
BUG-142: before and after
const GROUP_SIZE = 5;
const GROUP_DISCOUNT = 0.9; // pay 90% = 10% off
// Before (wrong): only 6 or more tickets got the discount
if (count > GROUP_SIZE) { price = price * 0.9; }
// After (right): 5 or more tickets get the discount
if (count >= GROUP_SIZE) { price = Math.round(price * GROUP_DISCOUNT); }

Math.round keeps the price a whole number of cents, as Act 03 taught.

Order matters: like in school maths, * and / happen before + and -. So 2 + 3 * 4 is 14, not 20. When in doubt, use brackets: (2 + 3) * 4 is 20. Brackets also make code easier to read.

Strict vs. loose equality in JavaScript: JavaScript has two equality operators. === (strict) checks that the value and the type are the same. == (loose) tries to convert types first, so "5" == 5 is true — which hides bugs like the one in the last chapter. Always use === and !==.

The fix for BUG-142 is one character: change > to >=. Now 5 tickets gets the discount, and so does everything above. Anna also replaces 0.9 with a named constant, GROUP_DISCOUNT, so the next reader instantly knows what the number means.

John adds one lesson that will come back all year: test the edges. Bugs hide at the exact boundary of a rule — 5 tickets, midnight, the last seat, an empty cart. Checking 4, 5 and 6 tickets would have caught this bug before any college paid too much.

Key Takeaway

An expression is any code that produces a value; operators do the work inside it — arithmetic (+ - * / %), comparison (> < >= <= === !==), logical (&& || !) and assignment (= += *=). Comparison bugs usually sit at the exact edge of a rule, so always test the boundary values.

Why This Matters

Almost every business rule in software is an expression: "5 or more tickets," "paid AND not cancelled," "student OR under 18." Getting one operator wrong quietly charges people the wrong amount or lets the wrong person in. Reading operators carefully, and testing the edges, is one of the most valuable habits a developer can have.

The bug is found and fixed. But while testing, Anna notices the pricing code only looks at one ticket at a time, and the group rule is tangled up with the student rule in a confusing way. To clean it up, she needs to understand how code makes decisions and repeats work.

Next