Asking About the Forecast and Warnings

Your port can now tell whether the wind sits in the club’s range. Ruth’s script asks two questions before it looks at the wind, though. Is a warning in force, and is the forecast thunder or fog? Either one cancels the race, however good the breeze.

Both checks lean on Python shortcuts. Ported line by line, each one runs without an error and answers a different question from the one Ruth meant, so nothing tells you the call is wrong. In this lesson, you’ll ask JavaScript the question you mean. Start with the forecast.

Ask Whether the Forecast Is Thunder or Fog

On the fog morning, the forecast word is "fog", and the race has to be canceled. Ruth’s script checks for both bad forecasts at once with in on a tuple:

Language: Python
forecast = "fog"
if forecast in ("thunder", "fog"):
    print(f"cancelled ({forecast} forecast)")

It prints cancelled (fog forecast). JavaScript has no tuples, so the fixed group becomes an array literal. To ask whether an array holds a value, you call its .includes() method, which compares values without type conversion and returns true when it finds a match:

Language: JavaScript
const forecast = "fog";
const badForecast = ["thunder", "fog"].includes(forecast);
console.log(badForecast);

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

Become a Member to join the conversation.