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

Nabeel Alam
Nabeel Alam
11,944 Points

How to have multiple correct answers?

If the question was: How many days in a week? And the correct answer was 7 or seven, is It possible to have two correct answers and how would we do it?

Thanks in advance

4 Answers

Colin Marshall
Colin Marshall
32,861 Points

Jonathan Grieve I think you have it reversed. He would use the OR operator in this situation. Your example will always be false because because $answer can never be both 1 and 'one' at the same time.

He's not saying the user enters two different answers and both of them have to be correct. He's saying that a user enters one answer and there are two things they could enter that would be correct. Therefore only one out of the two possible answer checks needs to return true to the conditional, so you would use the OR operator.

if (($answer == 1) || ($answer == "one")) {

}
Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Yes you can do that. The trick is to test for a condition that uses the AND operator

AND  or &&

so you could have something like

if($answer == 1)) && ($answer == "one") {

}

This would work because either one or both answers would pass the condition. The operator checks for both reponses rather than || which merely checks for one correct answer.

Nabeel Alam
Nabeel Alam
11,944 Points

Thanks Colin, thats the outcome I wanted!