Aim of the puzzle: Use a ternary operator in place of an if statement.
Walk through of solution: Use the first ternary as an example. Tap the area below where the
car variable is defined. Tap the buttons to put
car < train on this new line.
Make sure that
train is highlighted and tap the
? : button to attach the ternary operator to the comparison.
Now, if the comparison is true, the code on the left side of the colon
: will run. Add
print('Drive a car') to the section after the
? but before the
If the comparison is false, the code to the right of the
: symbol will run. In the space after the : symbol, add a
print() statement with the string ‘Buy train tickets’.
Sample code solution:
(Tap below to reveal)
var timeOffWork = 7; var daysNeeded = 5; timeOffWork > daysNeeded ? print('Go on vacation') : print('Go to work'); var train = 3; var car = 2; car < train ? print('Drive a car') : print('Buy train tickets');