Magic Eight Ball problem with Ternary Op?

Thanks to anyone who is willing to help.

So to make a long story short, I just finished writing the control flow for the Magic Eight Ball project in the JavaScript course.

For some reason, the ternary operator is not willing to function normally.

let userName = 'Patrick';
userName ? console.log(`Hello, ${userName}!`):console.log('Hello!');

Usually, the ternary operator starts with the is keyword. However, when I write is before the userName variable the console reads it as an error. When I take is out of the ternary it works perfectly fine.

So I am quite confused actually. Is the keyword is compulsory for the ternary operator?

Hey there,

No, not at all. is is in no way compulsory. The variable could be name, username, userName, USERNAME, UserName,… it wouldn’t change a thing. Except perhaps conventions and readability!

That said, this specific exercise might expect you to name your variable " userName " :wink:

1 Like

Thanks!

However, you’re saying it is not compulsory but when I type in is the console reads it as an error.

So would it be correct to say that it is not even required for the ternary?

The Codecademy console? That would be because the Learning Environment verifies that you’ve used the expected “userName” variable. But if you were to use the ternary operator outside of Codecademy, you could name your variable anything you want!


By the way:

I am reading this, so to modify your username,

Et voilà

1 Like

Uhm actually I see where I got confused :sweat_smile: :sweat_smile:

The variable name from my notes is isNightTime so this is what really gave me a hard time understanding. I kept on looking for the problem but actually there wasn’t. And to add to that confusion the variable name on MDN also started with “is”, so actually luck isn’t on my side this time. :joy:

And thanks for the username help, but it’s not that username I want to edit, in fact it’s the disgusting digital5974077886 I am trying to change but I’m guessing it is not possible.

:grin:

Actually using is makes a lot of sense.

let isNightTime = true;
isNightTime ? "time to go to bed then" : "keep coding a little more..."

You can then literally read it as English “It’s night time already? Well, time to go to bed then!”

isUsername would be slightly less obvious, you’d have to tweak to isUsernameProvided, or something? Giving good names to your variables is pretty important, for yourself and others reading your code!

As regards your username, you actually can change it, and yes I mean digital5974… :wink:

1 Like

Actually it does! That’s why I was completely puzzled, since the ternary checks for boolean value for me it was completely logical that is should precede the variable name as a part of the ternary operator, but apparently I was just confused.

Thanks so much for your time, really appreciate it!

To be honest, I don’t really see where to change my username :upside_down_face:

1 Like

My pleasure!

Right there, where it says Username :slight_smile:

But first, you should log out of the forums, then go to https://www.codecademy.com/account and put your new username there. Click Update Profile, and log back into the forums. That should work!

1 Like

You’re awesome man, I appreciate the love!

Finally can change my profile description. :joy: :rofl:

1 Like

Haha :laughing:

Much better username!

Edit: and description!

1 Like