Chapter 4Making Decisions

So far, the programs you’ve worked on have been somewhat simple. But sometimes you need to make a decision based on input from a user. And that’s where programming starts to get more challenging. Programs get longer and more complex, and testing them becomes more difficult. This is where test plans become even more important; to ensure correctness, you have to test all the possible ways the input can be interpreted.

So how do you make decisions in your programs? Most programming languages have an if statement where you compare a value to another value. In JavaScript, an if statement looks like this:

 
if​ (userInput === ​'Hello'​) {
 
// do something
 
}

If the input is Hello, then the code between the curly braces ...

Get Exercises for Programmers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.