Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Checking a Wind Range With &&

Checking a Wind Range With &&

Ruth’s chain had two postponed branches, one for too little wind and one for too much. She merges them, so the racing test reads like the club’s rule: the fleet races when the wind is from 6 knots up to, but not including, 25, in one chained comparison. The else picks the reason with a conditional expression:

Language: Python
wind_knots = 4
gust_knots = 7

if gust_knots >= 30:
    decision = "cancelled"
    reason = "gusts of 30 knots or more"
elif 6 <= wind_knots < 25:
    decision = "racing"
    reason = "good racing wind"
else:
    decision = "postponed"
    reason = "too little wind" if wind_knots < 6 else "too much wind"

print(f"{decision} ({reason})")

On the calm morning, 4 knots, Ruth’s script prints postponed (too little wind). In this lesson, you ported that range test to JavaScript, where a line that looks right can make the wrong call.

  • Your JavaScript port already had the old chain, with decision and reason declared with let above it, and each branch in braces. You updated it the same way Ruth did, and the reason became a ternary, with the condition first.
  • The comparison signs are the same in both languages, so the range test looks like it carries across as it’s written, 6 <= windKnots < 25. It runs with no error and no warning, but on the calm morning, the port posts racing (good racing wind), where Python posts postponed (too little wind).
  • JavaScript has no chained comparisons, so it reads the test left to right, one pair at a time: 6 <= 4 is false, and that false is compared with 25.
  • Less than compares numbers, so JavaScript converts the boolean: false becomes 0, and true becomes 1. 0 < 25 is true, so the test is true.
  • Python agrees with that step, (6 <= wind_knots) < 25 is True, since its booleans count as 0 and 1 too. But its chain compares the wind with each number, never a boolean with 25.
  • The first step only ever gives false or true, 0 or 1, and both are below 25, so the test is true for any wind. On the strong morning, 26 knots, Python postpones for too much wind, and the port races.
  • On the main morning, 14 knots, both race, so testing only that morning would miss the slip.
  • The same reading applies to plain numbers: JavaScript prints true for 1 < 5 < 3, because 1 < 5 is true, and true counts as 1, which is less than 3. Python prints False, because 5 isn’t less than 3. It can go the other way, too: 3 > 2 > 1 is false in JavaScript and True in Python.
  • Python’s chain is shorthand for two comparisons joined with and: 6 <= wind_knots and wind_knots < 25. JavaScript has no shorthand, so you write both comparisons in full and join them with &&, JavaScript’s and. The wind stays in the middle, between 6 and 25, so the test still reads as a range.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:02 Ruth is changing her script to decide racing with one wind range. In this lesson, you’ll port that test to JavaScript, where a line that looks right can make the wrong call.

00:14 Her chain has two postponed branches. She merges them, so the racing test reads like the club’s rule: six knots up to, but not including, twenty-five, in one chained comparison.

00:26 The else picks the reason with a conditional expression.

00:31 On the calm morning, Python prints postponed, too little wind.

00:38 Your JavaScript port still has the old chain, with decision and reason declared with let above it, and each branch in braces.

00:47 You update it the same way Ruth did. The comparison signs are the same in both languages, so the range test looks like it carries across as it’s written. The reason turns into a ternary, with the condition first.

01:02 Python posted postponed for four knots. What does the port post for the calm morning? Your turn: pause the video and predict.

01:17 It posts racing, good racing wind. There’s no error and no warning. The port sends the fleet out in four knots, where Ruth’s script waits for more wind.

01:30 JavaScript has no chained comparisons, so it reads the test left to right, one pair at a time. 6 <= 4 is false. That false is compared with 25.

01:46 < compares numbers, so JavaScript converts the boolean: false becomes 0, and true becomes 1.

01:54 0 is less than 25, so it’s true. Python agrees with that step, since its booleans count as 0 and 1 too.

02:04 But its chain compares the wind with each number, never a boolean with 25.

02:11 The first step only ever gives false or true, so 0 or 1, and both are below 25. So the test is true for any wind.

02:22 On the strong morning, twenty-six knots, Python postpones for too much wind. The port races again. On the main morning, fourteen knots, both race, so testing only that morning would miss the slip.

02:39 The same reading applies to plain numbers. What does JavaScript print for 1 < 5 < 3? Your turn: pause the video and predict.

02:55 It prints true. 1 < 5 is true, and true counts as 1, which is less than 3.

03:04 Python says False, because 5 isn’t less than 3. It can go the other way, too: 3 > 2 > 1 is false.

03:14 Python prints True.

03:18 Python’s chain is shorthand for two comparisons joined with and. JavaScript has no shorthand, so you write both comparisons in full and join them with &&, JavaScript’s and.

03:33 The wind stays in the middle, between 6 and 25, so the test still reads as a range.

03:40 On the calm morning, the first comparison is false, and && stops there: the test is false. The port posts postponed, too little wind.

03:53 On the main morning, both sides are true, so it races. On the strong morning, the second side is false, so it postpones for too much wind.

04:04 All three match Ruth’s script.

04:08 The rule: write each comparison in full, and join them with &&. Keep the value in the middle, so it reads as a range. Next up: the text lesson Asking About the Forecast and Warnings.

04:23 Before the wind, Ruth’s script asks about warnings and the forecast. You’ll port both checks so they ask what Ruth meant.

Become a Member to join the conversation.