Putting It All Together
You've learned inputs, math, choices, and more. Let's combine them in a tiny real-world example β figuring out a ticket price based on age.
Imagine a rule: little kids get in free, everyone else pays. We can write that
with an if:
function entryFee(age) {
if (age < 18) {
return 0;
} else {
return 5;
}
}
The < symbol means "less than." So young visitors return 0, and everyone else
returns 5.
Your task
Write a function ticketPrice that takes an age and returns the price:
- If the age is less than 5, the ticket is free β return
0. - Otherwise, the ticket costs
10β return10.
So ticketPrice(3) is 0 and ticketPrice(30) is 10.
β‘ 28 XP
π§± Code blocks
Drag a block into the editor, or tap it to drop it in where your cursor is. Add them in order to build your answer.
solution.jsRuns in your browser
Loading editorβ¦
Tests3 to pass
ticketPrice(3) is 0 (free for little kids)
ticketPrice(30) is 10
ticketPrice(5) is 10 (5 is not less than 5)
Run your code to check these.