thecodezone.
The Code Zone
Short steps, at your pace · Free lessons and practice · Member video walkthroughs
Save your next small win.

Checking your account…

09.05 · Ternary & conditional challenges / Practice · 6 min

Check the age boundaries

The ticket says 65, but the program picks the wrong category. Check the boundary.

Activity 5 of 6 in Ternary & conditional challenges

All courses

JavaScript Basics / 09.05

Check the age boundaries

Not started

Follow the original age-verification challenge and include both endpoints of its adult category.

More options
Rob compares three category cards and the boundary tickets 18 and 65.
Rob’s comic strip · Ternary & conditional challenges

Include age boundaries in a branch chain

Work through 4 focused steps. Read the explanation, type each requested change and check it before moving on. Your own code carries forward.

The mini-lesson opens in a new tab. Your own code carries forward; each step has a focused check and an optional hint.

Start mini-lesson(opens in a new tab)

4 steps · Saved work · Instant feedback

YOUR TURN

Check the age boundaries

Complete the if/else-if/else chain. Below 18 prints You are underage.; 18 through 65 inclusive prints You are an adult.; above 65 prints You are a senior citizen. Keep age = 65 for the browser check. This is a fictional categorization exercise, not a legal age rule.

Expected output
You are an adult.

Keep the starting inputs. Print these lines in this order, without extra output.

Edit JavaScript, then run it. Check Challenge shows which output checks pass or need a fix. Word wrap is enabled.

Ready to checkRun the checks to see your results here.

Work in VS Code instead

Copy the starter into a file named practice.js in VS Code. If Node.js is installed, open the terminal in that folder and run node practice.js. Compare the output with the task above. Automated challenge checks are available in the browser editor.

Code example
example.jsjavascript · example
const age = 65;

if (age < 18) {
  console.log("You are underage.");
} else if (age < 65) {
  console.log("You are an adult.");
} else {
  console.log("You are a senior citizen.");
}

Need a nudge?

Follow the chain

The second condition is reached only when the age is not below 18. Focus on its upper boundary.

Include the endpoint

The task includes 65 in the adult range. Compare < with <=.

Reveal a possible solution
One possible solution
example.jsjavascript · example
const age = 65;

if (age < 18) {
  console.log("You are underage.");
} else if (age <= 65) {
  console.log("You are an adult.");
} else {
  console.log("You are a senior citizen.");
}

Test 17, 18, 64, 65 and 66. The starting check is a boundary regression check, not full input validation. The original prompt-based version expects a valid nonnegative whole-number age; validation can be a later extension.

Replace your editor code?

Your current work will be replaced. Choose Cancel to keep it.

Open the code-along notes

From prompt input to a repeatable check

Your original challenge asks for an age using prompt() and parseInt(). Here a fixed age makes the browser check repeatable. After it works, try other valid whole-number ages.

In a local browser version you can replace the first line with const age = Number(prompt("Enter your age:"));. That version also needs cancellation, blank-input and invalid-number handling; the boundary exercise assumes a valid input. Use an if/else-if chain rather than nesting ternaries.

Values worth checking
example.jsjavascript · example
// Try these separately by changing the age input.
// 17: underage
// 18 and 65: adult
// 66: senior citizen

Ready to try it?

Practice in your browser and see your results.

Progress details

Have a question?

Ask the Code Zone community.

Ask a question in The Code Zone (opens in a new tab)

Continue learning

ONE STEP FORWARD

Check values on both sides of each boundary, including the boundary itself.