Basic JavaScript: Use the Conditional (Ternary) Operator HELP ME

Basic JavaScript: Use the Conditional (Ternary) Operator HELP ME
0

#1

Hi

I am struck here, please help. What is wrong with my code?

function checkEqual(a, b) {
a === b ? true : false ;
}

checkEqual(1, 1);

THANKS IN ADVANCE!


#2

Your function does not return anything, so undefined gets returned by default.


#3

Hi randell,
I have typed return in front of true and false, as well. I still don’t pass it.

function checkEqual(a, b) {
a === b ? return true : return false ;
}

checkEqual(1, 1);


#4

Now you are not using the ternary operator as it is intended and creating an error.

You want to return the result of the ternary expression. Ternaries are not just another way to write an if statement which is what you are attempting to do with the last code you posted.

Think about how you would return a simple expression like a === b which would automatically evaluate to a true or false value.


#5

I think I’m struck. I’ll refer to some videos on YT! Thanks randell!


#6

The documentation for the ternary (conditional) operator gives great examples.


#7

thanks man, I finally got it! :slight_smile:


#8

I am stuck on this as well; my code looks like this

return (a === b) ? True : False;

I really don’t know what’s wrong here. How did you finally solve it?
Thanks for your help


#9

@morginador you’re so close. the challenge is asking for true, not True, and false, not False.


#10

Hello, I’m having the same issue here is my code:
function checkEqual(a, b) {
return a === b ? “true” : “false”;
}

checkEqual(1, 2);


#11

Hey @rayramez,
Create a new topic with your issue by using “Ask for help” button the challenge page.

As far as challenge is concerned,
See there is a difference between “true” and true in JavaScript.
Here, former is string and latter is Boolean.
Try solving now.


#12

Thanks aditya_p, i found this out after I submited the question. Thanks


#13

@rayramez, glad you found the answer.
However from now onwards, whenever you need help with any of the Freecodecamp challenges, use the “Ask For Help” button on your challenge page. It gives the link to the challenge as well as posts the code properly here on forum.


#14

you did’nt use the return statement ,use the return statemnt then u got the answer !!!..


#15

Weird I have the same code as you and passed all the tests.

  return a === b ? true : false;
}

checkEqual(1, 2);```

Wait I see you wrapped true and false in quotes. Take those quotes off and it should work for you as well. I am still new to this as well so don't quote me but I believe if I am correct wrapping true and false in quotes converts them into strings and therefore would not be a boolean anymore.