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 trialRafael Vargas
3,339 PointsLets 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
Treehouse Moderator 27,696 PointsAnother 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.")
}
William Li
Courses Plus Student 26,868 PointsYou 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"
Jason Anello
Courses Plus Student 94,610 Points+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.
Matthew Toomb
3,246 PointsFunny 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 "===" ?
Rafael Vargas
3,339 PointsThank you very much.
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsThis 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
Courses Plus Student 26,868 PointsWilliam Li
Courses Plus Student 26,868 PointsOops, 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.Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsHey 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
Treehouse Moderator 27,696 PointsRich Donnellan
Treehouse Moderator 27,696 PointsGood call, Jason.
And, thanks to whoever gave me credit for 'Best Answer'. :-)
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsIt wasn't me :)
I think that should be left up to Rafael or William
William Li
Courses Plus Student 26,868 PointsWilliam Li
Courses Plus Student 26,868 PointsVery 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 :)
Rich Donnellan
Treehouse Moderator 27,696 PointsRich Donnellan
Treehouse Moderator 27,696 PointsThanks again, William Li!
William Li
Courses Plus Student 26,868 PointsWilliam Li
Courses Plus Student 26,868 Pointsdon't mention it, nice new avatar btw.
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsI'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.