Loading video player…

Decisions in JavaScript (Overview)

In this video, you got a first look at where this course goes. In it, your scripts learn to make choices.

In This Course, You’ll Learn How To

  • Translate stated rules into branches.
  • Combine conditions into one check.
  • Clean and validate input before you use it.

Along the way, you’ll write small scripts in VS Code, like a packing adviser for the weather, and practice on-site with automatic feedback.

What You’ll Build

A day-trip ticket checker. The on-site exercise passes it two sample answers, an age as text and a trip club membership answer, and your checker turns them into a price in cents, following a published rule table:

Visitor Price in cents
Under 5 0
5 to under 16 800
16 to under 65, trip club member 1200
16 to under 65, not a member 1600
65 and over 1000

Only ages from 0 to 120 count. When the age can’t be priced, the checker gives null. The video showed three visitors:

Age answer Member answer Result
"30" " Yes " 1200, the member price
"0" skipped 0, because babies ride free
" " (blank) "yes" null, nothing to price

You’ll build it on-site in two steps, with tests after each: first you check the answers, then you price the ticket. Then you explain two of its results.

Before You Start

You’ll need Node and VS Code with a course folder. You should be able to compare values, spot NaN, null and undefined, and tell numeric text from a number. Rusty on comparisons? The first lesson opens with a quick warmup.

This course comes with sample code and a cheat sheet. The sample code has the files you’ll create in the lessons, plus a starter and a solution for each exercise. Keep the JavaScript cheat sheet open as a quick reference while you work:

Download

Sample Code (.zip)

20.1 KB
Download

JavaScript Cheat Sheet (.pdf)

229.6 KB

Next up is Run Code Only Sometimes With if, where your scripts make their first choice.

Become a Member to join the conversation.