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

Porting the Race-Day Call Script

Porting the Race-Day Call Script

You’ve ported each part of Ruth’s race-day call on its own. In this project, you port most of her Python script into one file.

A port that runs can still make a different call, and a wrong one sends dinghies out in fog, so each line is written the JavaScript way, and for the main morning, the port prints Python’s first two lines. The members’ note, the racing-only course line and the waiting line are left for the exercise.

  • The readings become constants, one const each, because nothing reassigns them. The names turn into camelCase, Python’s False is false in JavaScript, and the empty list of warnings becomes an empty array.
  • Every branch sets the decision and the reason, and the headline reads them later. In Python, they outlive the branch, but a const inside a JavaScript branch ends at its brace, so the port declares both with let above the chain.
  • The first branch asks whether the warnings’ length is above zero, warnings.length > 0. Every JavaScript array is truthy, even an empty one, unlike Python’s empty list, so the list alone would cancel every morning, for an undefined warning.
  • Each elif becomes else if: an else followed by another if, in its own braces. Typed out of habit, elif stops the file at the brace with a SyntaxError.
  • The forecast check asks the array whether it includes the forecast, ["thunder", "fog"].includes(forecast). The in operator looks at an array’s indexes, not its values, so it would send the fleet out on the fog morning. The gusts carry over unchanged.
  • The wind range joins two full comparisons with &&. JavaScript has no chained comparisons, so Python’s form would race the calm morning’s 4 knots.
  • Anything else is postponed, and the reason comes from the conditional operator: the condition first, then the two values. In Python’s order, the line stops at the if with a SyntaxError.
  • On the calm morning, with 4 knots, gusts of 7 and the wind from the south, there are no warnings, the forecast is fair and the gusts are light, so the chain reaches the range, and 4 knots is under it. The conditional operator picks too little wind: postponed, as in Python.
  • The course comes from a switch over the direction. Directions that share a course stack their cases above one body and one break, and default takes the place of case _:.
  • In JavaScript, | in a case is bitwise OR, a number operator: case "N" | "NE": compares with 0, so it never matches. Without the breaks, northeast would fall through to the short windward-leeward. Since each case only picks a name, many JavaScript developers use a lookup object; the switch is equally correct, and lines up with Ruth’s match.
  • The session is one conditional expression, so it’s a const with its final value. It could sit inside the template’s ${}, as in an f-string. The headline’s f-string switches to a template literal: backticks for quotes, and a $ before each brace. For now, the course line prints on every morning.

Here’s the port, race-day-call.js, with the members’ note, the racing test on the course line and the waiting line still to come:

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:00 You’ve ported each part of Ruth’s script on its own. Now you’ll port most of it into one file. A port that runs can still make a different call, and a wrong one sends dinghies out in fog.

00:13 So each line is written the JavaScript way, and for the main morning, the port should print Python’s first two lines. The members’ note, the racing-only course line and the waiting line are left for the exercise.

00:29 The readings become constants: one const each, because nothing reassigns them. The names turn into camelCase, and Python’s capital False is a lowercase false in JavaScript.

00:40 The empty list of warnings becomes an empty array.

00:46 Every branch sets the decision and the reason, and the headline reads them later. In Python, they outlive the branch, but a const inside a JavaScript branch ends at its brace.

00:58 So the port declares both with let above the chain. The first branch asks whether the warnings’ length is above zero. Every JavaScript array is truthy, even an empty one, unlike Python’s empty list.

01:13 So the list alone would cancel every morning, for an undefined warning.

01:19 Each elif becomes else if: an else followed by another if, in its own braces. Typed out of habit, elif stops the file at the brace.

01:32 The forecast check asks the array whether it includes the forecast. The in operator looks at an array’s indexes, not its values, so it would send the fleet into the fog.

01:44 The gusts carry over unchanged.

01:48 The wind range joins two full comparisons with &&. JavaScript has no chained comparisons, so Python’s form would race the calm morning’s four knots.

02:01 Anything else is postponed, and the reason comes from the conditional operator: the condition first, then the two values. In Python’s order, the line stops at the if.

02:15 Take the calm morning: light wind, from the south. Which call does the finished chain make? Your turn: pause the video and predict.

02:30 Postponed: too little wind. No warnings, a fair forecast and light gusts, so the chain reaches the range, and four knots is under it. The conditional operator picks too little wind, as in Python.

02:47 The course comes from a switch over the direction. Directions that share a course stack their cases above one body and one break, and default takes the place of case _:.

02:58 In JavaScript, the | is bitwise OR, a number operator: that case compares with zero, so it never matches. Without the breaks, northeast would fall through to the short windward-leeward.

03:13 Since each case only picks a name, many JavaScript developers use a lookup object. The switch is equally correct, and lines up with Ruth’s match.

03:25 The session is one conditional expression, so it’s a const with its final value. It could sit inside the template’s braces, as in an f-string. The headline’s f-string switches to a template literal: backticks for quotes, and a dollar sign before each brace.

03:42 For now, the course line prints on every morning.

03:47 The port, step by step: the readings, the chain, the course and the headline.

03:56 node --check reads the file and says nothing: no syntax errors. Run it, and JavaScript prints two lines. They match Python’s first two.

04:09 The rule: write each condition as the question you mean, then check that the output matches. .length > 0 for emptiness, .includes() for one of several, and two comparisons for a range.

04:23 Next up: the exercise Finish the Race-Day Call. You’ll post the members’ note, print the course only on racing mornings, and add the waiting line until the race officer confirms.

04:34 Then the main, fog and calm mornings print what Python prints.

Become a Member to join the conversation.