FAQ: Conditional Statements - Ternary Operator

This community-built FAQ covers the "Ternary Operator " exercise from the lesson “Conditional Statements”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise _Ternary Operator _

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

8 posts were split to a new topic: Why is favorite_Phase === 'Love That!' required?

7 posts were split to a new topic: I don’t have the pre-generated code?

A post was merged into an existing topic: FAQ: Conditional Statements - Else If Statements

3 posts were split to a new topic: Can ternary operators contain multiple actions?

6 posts were merged into an existing topic: Why is favorite_Phase === 'Love That!' required?

A post was merged into an existing topic: Why is favorite_Phase === 'Love That!' required?

2 posts were split to a new topic: If ternary operators are the same as if/else, why use them at all?

We seemed to be forced to use the following:

isLocked ? console.log(‘You will need a key to open the door’) : console.log(‘You will not need a key to open the door’);

whereas, the following works and is a better way to do it but is not accepted. Just to let you know so you might want to fix it.

console.log(isLocked ? ‘You will need a key to open the door’ : ‘You will not need a key to open the door’);

It is the same for the other two as well.

2 Likes

Hello @scriptmaster34194! Welcome to the forum.
There are many lessons throughout Codecademy where the SCT is expecting only 1 of several possible solutions. I would imagine that it would be quite difficult to write the SCT code to accept every possible working solution that we could come up with. There are several other ways that learners could debate would be ‘better’ to pass the exercise that you’ve referenced. For example:

console.log(`You will${isLocked ? ' ' : ' not '}need a key to open the door.`);

Sometimes we just have to deal with what is, and move on. Happy coding!

3 Likes

Hi! I’m doing the Ternary operator lesson. The first two questions worked and ran fine, but when I do the third one, I get the error: Did you check to see if favoritePhrase is assigned to ‘Love That!’?. I have checked, and it is. My code runs fine, so why can’t I complete the lesson?

2 Likes

Hello, @script8242567609. Welcome to the forums.

Can’t really say without seeing your code.

favoritePhrase === ‘Love that!’ ? console.log(“I love that!”) : console.log(“I don’t love that”);

Is that phrase supposed to include an exclamation mark?

Yes, I think it does, at least the sample code has them.

That should fix your so called error. The text strings must match exactly to those given in the instructions.

1 Like

It definitely needs an exclamation mark.

1 Like

When there are multiple conditions, in PHP, i usually write code like this:

So it is more readable for me.

Ok so my third Ternary Operator statement is falling out in error. I am not sure where my formatting error is. Here are 2 screen caps:Screen Shot 2020-05-13 at 10.52.11 AM Screen Shot 2020-05-13 at 10.57.47 AM

screen

Problem is here.

1 Like