Learn JavaScript · lesson 12 of 60 · about 9 minutes
Project: Club Sign-up
Sam is starting a coding club. Read the age a visitor types, check the rules with comparisons and logic, and show who can join.
What you'll learn
- Combine conversion, comparisons and logic
- Check the edges of a rule
- Show the answer on the page
The rules
Sam's club has two rules:
1. You have to be at least 10
2. Under 16, you need a parent's OK
The sign-up form gives the age as text, ageText, and parentOk as true or false. The check converts the age, compares it, and combines the answers.
The check, step by step
const age = Number("12");
const parentOk = true;
const oldEnough = age >= 10;
const allowed = age >= 16 || parentOk;
console.log(`Old enough: ${oldEnough}, allowed: ${allowed}`);
console.log(`Can join: ${oldEnough && allowed}`);Output
Old enough: true, allowed: true
Can join: trueTest the edges
Rules break at their edges: 9 and 10, 15 and 16. Check those first. Is 10 in? Then the rule needs >=, not >.
And convert before comparing. Two strings are compared letter by letter, like words in a dictionary, so "9" >= "10" is true: 9 comes after 1.
Text compared like words
console.log("9" >= "10");
console.log(Number("9") >= 10);Output
true
falseTip
Naming the parts, like oldEnough and allowed, makes a long check easy to read and easy to test.
Now try it yourself
This lesson is free in Codiqo, with 9 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.