PatrickFrankAIU / ITWEB220-2404A

Learning resources for students in ITWEB 220, term 2404A.
0 stars 0 forks source link

Exercise: Ternary Operator #13

Open PatrickFrankAIU opened 3 months ago

PatrickFrankAIU commented 3 months ago

For this exercise, simply review some of our earlier examples of IF, ELSE, and ELSE-IF statements and convert them to use the ternary operator and related syntax. I've also placed some very simple scenarios below based on our earliest on-screen examples that you can try as well.

Info on Ternary: https://javascript.info/ifelse

Simple Example:

let number = 3;
console.log(number > 0 ? "The number is positive" : "The number is negative");

Nested Example with "else" (negative outcome):

let age = 8;
let message = (age < 9) ? 'Below age 3!' :
  (age < 18) ? 'Below age 18!' :
  (age < 100) ? 'Below age 100!' :
  'Over 100!';
console.log(message);

Scenarios: