From Zero to Deploy · Module 2 · Lesson 2.3

2.3 Conditions and loops, deciding and repeating

How code makes decisions with if and else, compares values, and repeats tasks with for without you copying anything.

⏱ 20 min

if: “if it’s like this, do that”

Every app makes decisions: if the cafe is open, show the green button; if the password is wrong, show a warning. In code, that’s if. else is what happens when the condition doesn’t hold.

Try it

Is the cafe open?

Console
Nothing in the console yet. Use console.log(...) to show values here.

The condition inside the parentheses always gives true or false. These are the comparisons you’ll see most:

Code Means
a === b a equals b
a !== b a is different from b
a > b, a >= b greater than, greater than or equal
a < b, a <= b less than, less than or equal
x && y x and y are true
x || y x or y is true (at least one)
!x not x (flips it: true becomes false)

Challenge: the cafe now also opens at night, from 20 to 23. Change the condition to (hour >= 7 && hour < 18) || (hour >= 20 && hour < 23) and test it with isOpen(21).

else if: more than two paths

When there are several possibilities, you chain else if. The code tests one condition at a time, top to bottom, and stops at the first one that’s true. That’s how the 3D game decides the color of the terrain:

Try it

What terrain is this?

Console
Nothing in the console yet. Use console.log(...) to show values here.

Challenges:

  1. Add snow on the mountain tops: replace the last else with else if (height < 60) { return 'rock'; } else { return 'snow'; } and test it with terrain(80).
  2. Now change the order: put the height < 30 test first. What does terrain(-5) return? Why did the sea disappear?

Order matters: -5 is also less than 30, and since that test comes first, it wins. When a chain of else if gives the wrong result, check the order.

Now it’s your turn. Write the code in the editor and click Run tests: the tests try your function with several numbers, including the ones on the edge, like 5 and 10.

Exercise

The badge

Finish the function badge with if, else if, and else. Think about the order of the tests: remember the sea that disappeared.

JavaScript
Tests
Click Run tests to check your code.
Console
Nothing in the console yet. Use console.log(...) to show values here.

= is not ===

One equals sign (=) puts a value into a variable. Three (===) compare two values. Mixing them up is a classic bug:

Find the bug

The prize should only come on the 3rd visit, but it shows up on the 1st. Which line has the problem?

Click the line with the problem.

You’ll also see == (two signs) in older code. It compares in a looser way that leads to surprises. Always prefer ===.

for: repeating without copying

for repeats a block of code several times. It has three parts, separated by ;: where it starts (let i = 1), how long it keeps going (i <= 5), and what changes on each lap (i++, which adds 1).

Try it

Counting up and counting down

Console
Nothing in the console yet. Use console.log(...) to show values here.

Challenge: in the first for, change the 5 to 20 and show only the even numbers. Put an if inside the for: if (i % 2 === 0) { console.log(i); }. % gives the remainder of a division: a number is even when the remainder of dividing it by 2 is zero.

Exercise

Free coffee

Put the two ideas of this lesson together: a for that counts from 1 to 12, and an if inside it. The text of each line has to be exactly like in the comment.

JavaScript
Tests
Click Run tests to check your code.
Console
Nothing in the console yet. Use console.log(...) to show values here.

Where you’ll see this in the course

  • In the 3D game, a for goes through every point of the terrain (thousands of them) and a chain of if / else if picks the color: sea, sand, forest, or rock. It’s the example above, only bigger.
  • Also in the game, an if checks whether the plane went below the ground or left the map (several conditions joined with ||) and, if so, calls restart(). And when passed === rings.length, you’ve flown through every ring and the race is over.
  • In the AI app, the server starts with if (!imageBase64 || !ACCEPTED_TYPES.includes(type)): if no photo came in or the type isn’t accepted, it turns the request down before spending anything on the AI. ! means “not”.
Task

Practice

0 of 5 done

Checkpoint

Check what you learned

  1. What is the difference between = and ===?

  2. When is hour >= 7 && hour < 18 true?

  3. How many times does this for run?

    for (let i = 0; i < 3; i++) {
      console.log(i);
    }
  4. In a chain of if / else if, two conditions are true. Which path runs?

Finished the lesson?

Mark it complete to track your progress.