Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics (Retired) Making Decisions with Conditional Statements Introducing Conditional Statements

Lets say somebody types in ruby insted of Ruby.

I tried with: if (answer === "Ruby", "ruby")

But, all answers are "correct". What is best solution in this situation.

Thanx in advance

3 Answers

Rich Donnellan
MOD
Rich Donnellan
Treehouse Moderator 27,671 Points

Another option is to convert the answer to either upper or lower case characters and check against that.

Example (using toLowerCase()):

var answer = prompt("What is the programming language whose logo is a type of jewel?").toLowerCase();
  if (answer === "ruby") {
    console.log("You are correct!")
  } else {
    console.log("Sorry, that isn't the correct answer. Please try again.")
}

This would be the preferred way to handle it.

There are too many possibilities to check for individually.

The user could type "Ruby", 'ruby", "RUBY", or even "rUby"

William Li
William Li
Courses Plus Student 26,868 Points

Oops, I was merely trying to correct the syntax error in the line if (answer === "Ruby", "ruby") posted on the question; but if that is what the questioner asking about, yeah, this would be the optimal solution.

Hey William,

Interestingly enough, the code posted doesn't have a syntax error. It produces a condition that is always true because of how the comma operator works in javascript. Rafael had mentioned that all answers are "correct".

The comma operator always returns the rightmost operand.

It's as if the condition was written like this if ("ruby")

This is always going to be true and whatever you type for answer doesn't factor into it.

I'm not aware of any practical uses for the comma operator. I read somewhere that you're unlikely to see it in a real code project.

So I would say that the original code contained a logic error and you did post the correct way to fix that logic problem.

Rich Donnellan
Rich Donnellan
Treehouse Moderator 27,671 Points

Good call, Jason.

And, thanks to whoever gave me credit for 'Best Answer'. :-)

It wasn't me :)

I think that should be left up to Rafael or William

William Li
William Li
Courses Plus Student 26,868 Points

I read somewhere that you're unlikely to see it in a real code project.

Very interesting, totally new to me, didn't know that's legal js code. thanks Jason, learned something new from this; but in all honesty, I've only seen such code in Ruby, not js.

Rich Donnellan , I did, you deserved the credit :)

I'm not that familiar with ruby so I don't know how it's used but hopefully it has a more practical use than in js.

With my luck, someone will come along now and show a super practical use for the comma operator in js.

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

You can use the OR logical operator || here.

if (answer === "Ruby" || answer === "ruby")

Now the if condition would evaluate to true when the answer is either "ruby" or "Ruby"

+1 because this does show how to correct the logic error and it would be the right thing to do in cases where you're only checking against a few possibilities.

Funny thing, before he covered it in the video with UpperCase, I wondered what would happen if someone typed "ruby". So I paused the video to visit StackFlow - found a very similar solution :

if ((answer == 'Ruby' ) || (answer == "ruby")) { document.write("<p>That's correct!</p>"); }

So what's the difference between "==" and "===" ?

Thank you very much.