Exercise 1.2: Ternary Operator
Level 1
Exercise 1: Basic ternary operator: Write a function canDrive
that accepts the age as a parameter and uses the ternary operator to determine if the user can drive. If the age is 18 or more, it should return 'You can drive'. Otherwise, it should return 'You cannot drive'.
Exercise 2: Use with comparison operators: Write an expression using the ternary operator to determine which of two given numbers (num1
and num2
) is greater. If num1
is greater, return 'num1 is greater'. Otherwise, return 'num2 is greater'.
Level 2
Exercise 3: Chained ternary operators to determine if a number is positive, negative, or zero.
Ternary operator with functions: Create a function findMax
that accepts three parameters (a
, b
, c
) and uses the ternary operator to determine the maximum value.
Level 3
Exercise 4: Ternary operator inside a loop: Write a function evenOrOdd
that takes an array of numbers and uses a loop to iterate through the array. Inside the loop, use the ternary operator to determine whether each number is even or odd.
⚠️ Important: Open the browser console (F12 or Ctrl+Shift+I) to see the results of the exercises and verify that they work correctly.