Why is `favorite_Phase === 'Love That!'` required?

thanks for reply but still cant get it to work cheers i give up :woozy_face:

I kind of understand this, but I’m still unsure of it a little bit. I had trouble with this exercise too. Since the operator === means is equal to, is the reason why we have to put p === ‘Love That!’ before the ternary operator, because the value is not assigned to the variable P?

The condition being tested is whether or not the variable p is assigned to the string literal 'Love That!'. If it is, the value immediately following the ternary operator is returned. If it isn’t, the value following the : is returned instead. I wrote that answer some time ago, and it’s not really a good example of using a ternary expression. The following would be better:

let p = "Love That!";

console.log(p == "Love That!" ? "I love that!" : "I don't love that!");

// if we wanted to assign a value using the ternary expression we could do this

p = "meh" // change the value p is assigned to

const response = p === 'Love That!' ? "I love that!" : "I don't love that!";

// then we can print response or do whatever else with it

console.log(response);

Output:

I love that!
I don’t love that!

In the exercise if you do not use favouritePhrase === ‘Love that!’ then the statement will always evaluate to TRUE if there is any string in the variable, this is because string attached to a variable is a truthy. The only way you would get the ‘I don’t love that’ response would be with blank string (’ ') being stored in the variable. This way you are telling the program to check if the string is exactly the same as what is stored in the variable :slight_smile:

1 Like
let favoritePhrase = "Love That!"; favoritePhrase === "Love That!" ? console.log("I love that!") : console.log("I don't love that!");

This is the perfect explanation to that piece of logic!! I got it wrong so many times until I saw this example! :)))))

1 Like

Yo en tiendo así:
cuando escribimos console.log(‘I love that.’) eso es lo que le mostramos o imprimimos a la consola del navegador en mi caso google Chrome.
Para que eso pudiera imprimir se tuvo que la variable let favoritePhrase fuera igual a love that y se cumpliera que fuera verdadero.
en pocas palabras nosotros podemos declarar variables y imprimir a la consola cualquier cosa que se nos ocurra siempre y cuando esas variables sean igual definida al proceso del operador ternario ya que la computadora no sabe o no tiene un pensamiento lógico en cuanto a lo que se imprime o sea el console.log()

I thought the same thing as cgg, that the past two examples were favouratePhrase ? console-log(‘I love that’) : console.log (“i don’t love that”); I assumed that the first case was true and the second was false. I had the idea and was going well understanding these statements, until i saw the answer. then confusion set in. Just a little confused. It it is an exception to the rule then ill just have to understand.
Thanks Cgg, mtf, chipblaster, and others.

so after reading the other reply on here i figured out the answer but i feel like a more concise explanation for those who come after me wanting to know the same thing wont go amiss.

the reason the correct solution to the 3rd problem is
favoritePhrase === “Love That!” ? console.log(“I love that!”) : console.log(“I don’t love that!”)

and not
favoritePhrase ? console.log(“I love that!”) : console.log(“I don’t love that!”)

is that the variable favoritePhrase does not have an inherently true or false statement to it unlike the other two variables in this example which are
let isLocked = false;
let isCorrect = true;

isLocked is false and isCorrect is true we don’t need to ask questions for these since they are the answer.

where as favoritePhrase is a string and therefore needs a question for this exercise.
now the reason why you don’t get an error and even get the right output is because lacking a defining question to ask favoritePhrase it defaults to a truthy falsey check which is just asking if favoritePhrase exists at all as any kind of information

1 Like

From reading this whole thread I think this is really the most helpful example. This explains precisely why the identity operator is needed. Thanks for the great answer :+1:

2 posts were split to a new topic: Why the exercise returns ‘I love that!’

I think both cgg and mtf are correct!

let favoritePhrase = ’ Love That!’;
// either 1.
console.log(favoritePhrase === ‘Love That!’ ? ‘I love that!’ : “I don’t love that!”);
// or 2.
console.log(favoritePhrase ? ‘I love that!’ : “I don’t love that!”);

Correct me if I’m wrong! The string value ‘Love that’ is committed to the memory at variable favoritePhrase. The ternary 1 would provide more clarity to the user/programmer. Whereas ternary 2 would also provide the correct answer as the favoritePhrase is truthy for it contains a string value ‘Love that!’ but nothing else (you can test with a different value and see the response); it, however, isn’t clear to a human reader as to what value it was assigned to (unless one looks at the previous lines of codes) until the program finishes its execution, especially in a very long program.

mtf explains that the program doesn’t know its dynamic state, true… until the result is assigned (committed) to a memory pointer. Unless its value is changed intentionally, otherwise it will be the same throughout the program when the same variable is called for. Again, only the computer “knows” what it holds in the memory, it might be a better practice for us to provide clarity!

Updated:
I’ll use an example of a website that requires a user to prove who he says he is before logging in. The website asks for the user’s social security number against its database of thousands of members:

let userInput = (user input here);
const userSocialSecurityNo = ‘123-45-6789’;

console.log( (user input) === userSocialSecurityNo ? ‘Welcome aboard!’ : ‘Please try again!’ );

Had it been just an evaluation of and by itself (user input) only, anyone could log into the website with a fake number (except zero or empty, which is falsy)!
In the case of the example illustrated by the lesson, there’s only one value committed to the memory (i.e., “Love That!”) so though

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

provides the answer, but it’s undesirable for safe practicing, IMO.

1 Like

I appreciate the Hitchhikers Guide to the Galaxy reference :smiling_face_with_tear:

1 Like

Hi all, the way I see it is as follows:

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

The above works when executed in VScode and should be the preferred solution (in my view). As favoritePhrase has already been assigned a valid string automatically makes it a truthy value, hence “I love that!” will be logged to the console. Making use of an assignment operator in the solution (in this case) only makes it more verbose and isn’t necessary, defeating the purpose/real benefit of a ternary.

No, the reason we need the assignment operator (===) is because we are not just asking if favoritePhrase is true or false in this instance. We are asking if favoritePhrase “is equal to” what it was called out to be, as in is identical to.

As long as favoritePhrase is not an empty string, the following code will always read favoritePhrase as true and show ‘I love that!’

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

However, if we want the computer to actually read the string and make sure it “matches” the string assigned to favoritePhrase, we would need to write the code as such:

favoritePhrase === ‘Love That!’ ? console.log(‘I love that!’) : console.log(‘I don’t love that!’);
or even simpler,
console.log(favoritePhrase === ‘Love That!’ ? ‘I love that!’ : I don’t love that!’);

This way, if favoritePhrase is input as any string other than ‘Love That!’, the second statement would be returned.

Hope this helps.

1 Like

Agree. This was the only answer that finally helped me understand why the first 2 did no require what the 3rd did. Thank you so much!

The assignment operator is needed because you are testing whether or not the value in favoritePhrase equals ‘Love That!’.

Imagine if we set favoritePhrase as a value such as, "I really don’t love that’. The conditional would still be evaluated as Truthy (because strings are Truthy) and will output “Love That!” – clearly not what we want.

So, we use the assignment operator before the ? (ternary operator) to check whether or not favoritePhrase = “Love That!”. If it they equal to each other, then the conditional evaluates as true and outputs “Love That!”

Don’t think the lesson was very clear here. Codecademy should probably explain that when comparing two strings, an assignment operator needs to be used, even if it is before a ? (ternary operator).

1 Like

Thank you!
Many thanks for not adding a bunch of extra yadda yadda yadda to make the answer confusing!!!
Now I understand!

Okay, so if I understood this correctly, on this part :

let favoritePhrase = ‘Love That!’;

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

You still need a condition to execute this string of code, else it would always return false. Or we can ditch the favoritePhrase === ‘I Love That!’ part in favor of just favoritePhrase ?. I am so confused.