Debugging Conditionals
The program below reads a day's UV index, a whole number, and is meant to print one label for it. Readings 0 to 2 are low, 3 to 5 moderate, 6 and 7 high, and 8 to 11 very high, and any other number is invalid. That is a simplified scale: the published one calls 11 and above extreme. This lesson traces inputs through it to find its bugs one at a time, and repairs each.
let uv = Number(prompt());
if (uv < 0 && uv > 11) {
console.log("invalid");
}
if (uv <= 2) {
console.log("low");
}
if (5 <= uv <= 3) {
console.log("moderate");
}
if (uv >= 6 || uv < 7) {
console.log("high");
}
if (uv >= 8) {
console.log("very high");
}
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.
Given 4, JavaScript runs it without an error message and prints two labels, moderate and then high. A reading of 4 should get one label, moderate. Run the program above and type 4 to see both labels printed. As the lesson finds each bug, repair it in the program and run it again with the reading the lesson traces.
Two comparisons for a band
JavaScript reads the moderate test,
5 <= uv <= 3, from the left, as (5 <= uv) <= 3. At 4 the first comparison is false, which JavaScript converts to 0 before comparing it with 3. Since 0 is at most 3, the test is true.For any reading the first comparison is true or false, so the second compares 1 or 0 with 3. Thus the test is true for every reading. Given 10, the program prints moderate, high, and very high.
Putting the bounds in order, as
3 <= uv <= 5, doesn't help, since 1 and 0 are also at most 5. The mirror spelling, 5 >= uv >= 3, is false for every reading instead, since neither 1 nor 0 is at least 3.With && the order of the bounds still matters:
5 <= uv && uv <= 3 is true for no reading, since no number is both at least 5 and at most 3. The band is 3 to 5, so the repaired line is if (3 <= uv && uv <= 5) {. The output at 4 doesn't change, but moderate now prints only for readings from 3 to 5.Never true, always true
The high line tests
uv >= 6 || uv < 7, which is true at 4. It's true for every number, since a number below 6 is also below 7, so high always prints.The invalid line,
uv < 0 && uv > 11, fails the opposite way. No number is below 0 and above 11 at once, so invalid never prints.The chained moderate test was a condition of the second kind, and its mirror one of the first. Joining the high line's two comparisons with && instead gives
6 <= uv && uv < 7, and 4 now prints moderate alone.Negating a condition
A valid reading satisfies
uv >= 0 && uv <= 11, so the invalid line was meant to test !(uv >= 0 && uv <= 11). To remove the !, the programmer negated each part and kept the &&. At -3 that line is false, and the program prints low.By the first law, the invalid test is
uv < 0 || uv > 11: each comparison becomes its opposite, and && becomes ||. The two forms agree for every number, but not for NaN, the value Number() gives for a typed word. Every comparison with NaN is false, so only the form with ! catches it, and the program keeps that form.Separate ifs and a chain
With the invalid line repaired, -3 prints invalid and then low, although the invalid, low, and moderate conditions are each right on their own. The five ifs each check their condition, whatever the earlier ones found, and at -3 both the invalid test and
uv <= 2 are true.The labels are alternatives, so they belong in one chain, which prints exactly one label.
let uv = Number(prompt());
if (!(uv >= 0 && uv <= 11)) {
console.log("invalid");
} else if (uv >= 8) {
console.log("very high");
} else if (6 <= uv && uv < 7) {
console.log("high");
} else if (3 <= uv && uv <= 5) {
console.log("moderate");
} else {
console.log("low");
}
Now -3 prints invalid alone. The invalid test goes first because a reading above 11, such as 15, passes the very high test too. When two tests can both be true for one reading, only the first one's block runs, so the more specific test goes above the other.
Testing the boundaries
The label changes between 2 and 3, between 5 and 6, and between 7 and 8, and the valid range ends at 0 and 11. None of the readings traced so far lies on an edge, so they can't show a mistake there.
| Input | Meant to print | Chain prints |
|---|---|---|
| -1 | invalid | invalid |
| 0 | low | low |
| 2 | low | low |
| 3 | moderate | moderate |
| 5 | moderate | moderate |
| 6 | high | high |
| 7 | high | low |
| 8 | very high | very high |
| 11 | very high | very high |
| 12 | invalid | invalid |
At 7,
uv < 7 is false, and 7 is outside the moderate band, so the chain falls through to else and prints low. A reading of 6 passes the high test, so only the boundary itself shows the bug. With that test repaired to uv <= 7, all ten rows agree.