JavaScript from absolute zero · Chapter 3 of 14 · Languages · new chapter every Tuesday morning
By the end of this chapter: Write correct conditional logic and avoid the comparison mistakes the language invites.
The problem
Say you are a junior developer at a regional airline. You have been tasked with writing a small script for the check-in desk that calculates baggage fees. The rule is simple: if a suitcase weighs more than 20kg, the passenger must pay a £40 surcharge. If it is 20kg or less, it is free.
You write a script that takes the weight and prints the result. However, because you haven't yet mastered how to make the computer choose between two paths, your script prints "Surcharge applied" for every single passenger.
On a busy Saturday morning at Gatwick, 400 passengers check in. The staff, following the script's output, charge every person £40, including those with small carry-on bags. By 10:00 AM, the customer service desk is swamped with 150 angry travellers demanding refunds.…