Roads or Rails explainer


#1

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 : symbol.

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');

Javascript Concepts: Variables, Ternary Operator, Comparison Operators

Grasshopper Concepts: print()


#4

I tried everything like this and nothing mine looks exactly like the one underneath on yours :frowning_face:


#5

Could you take a screenshot of your code and then share that here?

–Frankie


#6

Uuuuh i did the code right and it didn’t work


#7

Could you post a screenshot of your code? Are you getting any blue popup hints telling you to correct something?

–Frankie


#8

@Cutieuni1 @SwiVVeRRz

I was looking at the puzzle again, and I found that there was some feedback that’s misleading. The ternary __?__:__ should have a comparison in the 1st blank, and then a print() in each of the other 2 blanks. But if the last print() is missing, the text says to add it to the middle blank – it should say to add it to the last blank. I’ll make sure this feedback gets fixed.

The solution in the original post should still work though. If you’re still having trouble, I could take a look at your code if you post it here.

–Frankie