1//ternary operator syntax and usage:
2condition ? doThisIfTrue : doThisIfFalse
3
4//Simple example:
5let num1 = 1;
6let num2 = 2;
7num1 < num2 ? console.log("True") : console.log("False");
8// => "True"
9
10//Reverse it with greater than ( > ):
11num1 > num2 ? console.log("True") : console.log("False");
12// => "False"
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))
1
2var numOfBottles = 99;
3
4while(numOfBottles > 0){
5
6 // ternary use, instead of using if / else
7 var bottles = numOfBottles < 2 ? "bottle" : "bottles";
8
9 console.log(numOfBottles + " " + bottles + " of beer on the wall.");
10 console.log(numOfBottles + " " + bottles + " of beer,");
11 console.log("take one down, pass it around,");
12 numOfBottles--;
13
14 console.log(numOfBottles + " " + bottles + " of beer on the wall.");
15}
1condition ? exprIfTrue : exprIfFalse
2
3//Example
4let age = 26;
5let beverage = (age >= 21) ? "Beer" : "Juice";
6console.log(beverage); // "Beer"