1// ternary operators are frequently used as a shorter cleaner if statement
2// condition ? exprIfTrue : exprIfFalse
3
4let age = 15;
5let canDrive = age >= 16 ? 'yes' : 'no';
6// canDrive will be 'no'
7// the condition will be age > 16 which is false so canDrive will equal exprIfFalse
8
9// this ternary is the same as this if else statement
10let age = 15;
11let canDrive;
12if (age >= 16) {
13 canDrive = 'yes';
14} else {
15 canDrive = 'no';
16}
17
18// canDrive will be 'no' because 15 is less than 16
19
20
1let startingNum = startingNum ? otherNum : 1
2// can be expressed as
3let startingNum = otherNum || 1
4
5// Another scenario not covered here is if you want the value
6// to return false when not matched.
7//The JavaScript shorthandfor this is:
8let startingNum = startingNum ? otherNum : 0
9// But it can be expressed as
10let startingNum = startingNum && otherNum
1// Write your function here:
2
3const lifePhase = (age) => {
4 return age < 0 || age > 140 ? 'This is not a valid age':
5 age < 3 ? 'baby':
6 age < 13 ? 'child':
7 age < 20 ? 'teen':
8 age < 65 ? 'adult':'senior citizen';
9}
10
11
12
13
14 console.log(lifePhase(5))