Making the Call With else if and the Ternary

Ruth’s script posts the call as a headline such as “Saturday morning race: racing (good racing wind)”. Her first draft looks only at the wind: strong gusts cancel, too little or too much wind postpones, and anything else races.

The port has to make exactly her call, yet one thing in this chain doesn’t carry over line for line. You’ll port it, then pick the headline’s “morning” or “afternoon” with JavaScript’s conditional operator.

Port the Chain With else if

The logic carries over unchanged: the first true condition wins, as in Python. The shape changes:

Python JavaScript
if gust_knots >= 30: if (gustKnots >= 30) {
elif wind_knots < 6: } else if (windKnots < 6) {
else: } else {
indented body body in braces

The parentheses mark where the condition ends, the job Python’s colon does. JavaScript ignores indentation, so braces mark where each branch ends, and in practice you always write them. There’s no elif because any statement can follow else, including another if.

Ruth’s Python sets decision and reason in every branch and prints them after the chain. The port declares both names first:

Language: JavaScript
const windKnots = 14;
const gustKnots = 19;

let decision;
let reason;
if (gustKnots >= 30) {
  decision = "cancelled";
  reason = "gusts of 30 knots or more";
} else if (windKnots < 6) {
  decision = "postponed";
  reason = "too little wind";
} else if (windKnots >= 25) {
  decision = "postponed";
  reason = "too much wind";
} else {
  decision = "racing";
  reason = "good racing wind";
}

console.log(`${decision} (${reason})`);

For the main morning, the port agrees with Ruth’s script:

Language: Program Output
racing (good racing wind)

Those two let lines are the one thing that doesn’t carry over line for line. A Python name set in a branch outlives the chain, but a JavaScript variable declared with const or let goes out of scope at its block’s closing brace. Declare const decision in each branch instead, and console.log(decision) stops with ReferenceError: decision is not defined.

Declared above the chain, decision and reason hold undefined until a branch assigns them. The final else guarantees an assignment. Without it, a missed case would print undefined where Python would raise NameError.

Three habits to drop while porting:

  • elif stops the file with SyntaxError: Unexpected token '{', with the caret on the brace, so look left of it. Write } else if (...) {.
  • Indentation is layout only. An indented line after a closing brace runs regardless of the condition, so the braces determine which lines belong to the branch.
  • = in a condition assigns instead of comparing. Python rejects this syntax. With a const variable like windKnots, that stops with TypeError: Assignment to constant variable. With a let variable, it changes the value, and the branch runs whenever the new value is truthy. JavaScript’s truth test differs from Python’s, as you’ll see in Asking About the Forecast and Warnings. Compare with ===.

The chain now posts the decision and its reason. The headline still needs “morning” or “afternoon”, a choice that only picks a value.

Pick a Value With the Conditional Operator

Ruth’s Python picks the session in one line, session = "morning" if start_hour < 12 else "afternoon". JavaScript’s conditional operator, or ternary, puts the condition first, then ? and the value for a truthy condition, then : and the value for a falsy condition. As in Python, only the chosen expression is evaluated. As an expression, it also drops into ${} like Python’s in an f-string. The main morning’s race starts at ten:

Language: JavaScript
const startHour = 10;
const session = startHour < 12 ? "morning" : "afternoon";
console.log(session);
console.log(`Saturday ${startHour < 12 ? "morning" : "afternoon"} race`);

Both forms pick the morning, and a start hour of 14 would pick the afternoon:

Language: Program Output
morning
Saturday morning race

With the chain’s decision and reason, one template literal makes the main morning’s whole headline, Saturday ${session} race: ${decision} (${reason}), which the script posts as “Saturday morning race: racing (good racing wind)”.

Writing the ternary in Python’s order stops with SyntaxError: Unexpected token 'if' because if is only a statement in JavaScript and can’t sit inside an expression.

A Python name set in a chain outlives it at no cost. In JavaScript, the ternary saves the let declared above the chain and makes session a const with its final value.

Keep it to one level of nesting. Past that, a ternary inside a ternary leaves you pairing each ? with its :, where an if chain shows the logic in braces. When branches do things or set two names as Ruth’s chain does, write an if chain with its names declared above it.

In the next exercise, Post the Call for Each Morning, you’ll finish a teammate’s port of the first draft so it posts the decision and its reason for the main, calm, gusty, and strong mornings. Each headline gets “morning” or “afternoon” from the start hour, matching Ruth’s Python script line for line.

Become a Member to join the conversation.