Exercise 4 of 10 · Conditions
Umbrella or Not
What you will make
A rain report card that holds today's forecast up against your own umbrella mark and signs off with one of two answers: take it, or leave it at home.
The one new idea: if/else chooses between two outcomes
A program that cannot choose can only recite. Every low-battery warning, every free-delivery message, every seat-belt chime is two values being compared and one of two outcomes being picked, and this is the shape that choice takes in JavaScript.
Go straight to the code ↓Asking a question before acting
Everything you have written so far ran once, top to bottom. Right for a banner, useless for advice, because advice depends on the day.
if is how a program asks before it acts. The question goes in round brackets; the lines to run when the
answer is yes go in curly braces straight after. Add else and a second pair of braces for the no. Exactly
one block runs — never both, never neither.
The braces are what matter: they alone decide which lines belong to the question.
if (somethingIsTrue) {
// inside the block
}
// outside it, and runs either wayJavaScript ignores the spaces at the start of a line. You indent because it makes the shape obvious to a human; the braces are the only thing JavaScript reads. If you have met Python, that is the exact reverse. Indent for yourself, brace for JavaScript.
One equals sign, two, or three
Inside the round brackets goes a comparison. === asks whether two values are the same, and >= asks whether
the left one is at least as big as the right.
One equals sign is not on that list. That is the trap.
=stores.const rainChance = 70;puts 70 under that name. It changes something.===compares.rainChance === 70asks whether what is under that name happens to be 70. It changes nothing.
Write one where you meant three and JavaScript tries to store. Both values here were made with const, so it
stops with TypeError: Assignment to constant variable. — and only after the card above has printed, because
JavaScript runs down the page until it hits trouble.
A third form, ==, compares loosely: it converts one side first, so the text "50" counts as equal to the
number 50. === checks the value and the kind of value together, and says no. This site uses ===
everywhere, because fewer surprises beats a shorter line.
A worked example
A lift rather than a weather report, so the answer here stays yours to write:
const floor = 3;
const groundFloor = 0;
console.log("Lift is on floor", floor);
if (floor === groundFloor) {
console.log("Doors open to the street.");
} else {
console.log("Going down first.");
}
console.log("Lift check done.");Lift is on floor 3
Going down first.
Lift check done.3 is not 0, so the first block is skipped whole and the second one runs. The last line sits outside both
pairs of braces, so it prints either way. Change floor to 0 and the middle line switches to the other
block's, while Lift check done. prints exactly as before.
Your turn
The editor holds a rain report. rainChance is today's forecast; umbrellaMark is the point at which an
umbrella is worth carrying. Two-word names in JavaScript are written like that: second word capitalised,
no gap.
Press Run first. The card appears, then advises leaving the umbrella at home on a day with a seventy percent chance of rain, because the question currently reads:
if (rainChance === 0) {which asks whether the chance is exactly zero: a fair question, and nothing to do with umbrellas.
Replace it with one asking whether rainChance has reached umbrellaMark: at the mark or above counts as
take it. Leave the braces and everything inside them as they are.
Then change rainChance to 20 and run it again. The same program should now send you out without one —
proof the advice is worked out from the numbers rather than written in.
If something goes wrong
The likeliest slip is the equals signs. With one, the card prints and the program then stops on
TypeError: Assignment to constant variable. — JavaScript was told to store into a name const had fixed.
Count them; a question needs three.
Next is a brace. Drop the { from the if line and only the next single line counts as the block, which
leaves the } further down closing nothing: JavaScript stops with SyntaxError: Unexpected token '}' before
running a thing. Take that } away too and the complaint moves to SyntaxError: Unexpected token 'else'.
Braces come in pairs.
If the card prints but the advice is wrong, nothing is broken. Read your question out loud as a sentence and check it is the one you meant to ask.
Write your code
Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.
Press Esc then Tab to move keyboard focus out of the code editor.
Output will appear here after you run your code.The runtime is starting in the background. You can type now — it will be ready before you are.
The answer appears here once you have run your code at least once.
Things that often go wrong here
- Writing one equals sign instead of three
- One equals sign stores a value rather than asking about it, so JavaScript tries to store. Both names here were made with const, which fixes them, and the program stops with TypeError: Assignment to constant variable. Notice that the card above has already printed by then: JavaScript runs down the page and stops where the trouble is, rather than checking everything first.
- Using two equals signs rather than three
- It does not rescue the answer here: 70 is not 50, so two signs and three signs both say no and the card still advises leaving the umbrella at home. The habit matters anyway. Two signs compare loosely, converting one side first, so the text 50 counts as equal to the number 50. Three signs compare the value and the kind of value together. This site writes three everywhere, because fewer surprises is worth more than a shorter line.
- Deleting the brace at the end of the if line
- Without it only the single next line counts as the block, so the closing brace further down is left closing nothing. JavaScript stops before running anything at all and reports SyntaxError: Unexpected token '}', so the card never appears either. Delete that closing brace as well and the complaint moves on to SyntaxError: Unexpected token 'else'.
- Comparing against a typed-in 50 instead of the name at the top
- The advice is right today and wrong tomorrow. Change the umbrella mark to 80 and the question carries on asking about 50, because that number was written into the question rather than looked up when the question was asked.
Want a blank editor instead? Open the JavaScript playground.