Roads or Rails explainer

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()

6 Likes

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

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

–Frankie

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

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

–Frankie

1 Like

@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

2 Likes

@Grasshopper_Frankie this should also work but it doesn’t though it would be the easiest way to program it.

3 Likes

We can modify the checks to allow that solution as well. Thanks for the suggestion!

–Frankie

1 Like

I am having the same problem as they are. My code is right but I am given hints like I am wrong. I don’t know Am I?

1 Like

Here is my screenshot

Hey there,

The code you have is valid and will work, but the puzzle is looking for this solution:

train > car ? print('Drive a car') : print('Buy train tickets');

We’ll take a look at updating this puzzle to allow for the solution you have, but for now, changing the code will solve the problem.

Hope this helps!
Ben

2 Likes

I had that solution first and got the same response. It finally worked after I went and reset the puzzle but I did have the same solution you showed me.

Hmm, it’s possible that there may have been something else going on then. If you experience anything similar in the future, try submitting a bug report from the app.

A bug report will send the app’s logs to us which we can use to figure out if something’s a bit wonky on our end.

Glad to hear you were able to solve the problem!
Ben

I did the right code but it says that I need to have the ‘Buy train tickets’ after the colon But I did that.Help?

Here is a screenshot

Hello, dear grasshopper and scholars, I am stuck here! Please help me

I don’t think if I am allowed to solve your queries but I gotta try:

I don’t think you have to include 2 ternary operators I will try to break the solution down:
The solution is this:

car < train ? print('Drive a car') : print('Buy train tickets');
So, if the value saved in the variable car is smaller than the value saved in the variable train, it will print the string “Drive a car”. If not, then it will print the string “Buy train tickets”.

There is also another solution that you might find more different:

print(car < train ? 'Drive a car'' : 'Buy train tickets');
So if the value saved in the variable car is smaller than the value saved in the variable train it will return the string “Drive a car”. If not then it will return the string “Buy train tickets”.

Don’t worry if you don’t understand the new keyword return; you will learn it later in the course (I think) and it is also a very big part in creating functions; another fundamental topic in JavaScript you will learn later in the course (I am not sure 'cuz I haven’t completed the course)

Hope this helped you on your coding journey!

it is showing my code is wrong
but I actually wrote it correct
here is my screenshot