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 trialSergio Morales
455 PointsThe OR || operator doesn't work on this project, why?
I tried to add the || to a quiz to give my user two options but when I use it even if I put the wrong answer it counts it as correct. When I take off the second option it works fine. Can I not use the || operator in this instance?
let myAngeles = prompt(
`Easy start to the quiz ${myName}, give me any NFL team from LA?`
);
if (myAngeles.toUpperCase() === `RAMS` || `CHARGERS`) {
myScore += 20;
}
4 Answers
Jordan Laursen
10,465 Pointslet myAngeles;
const myAngelesPrompt = () => {
myAngeles = prompt( `Easy start to the quiz ${myName}, give me any NFL team from LA?` );
if(myAngeles.toUpperCase() === `RAMS` || myAngeles.toUpperCase() === `CHARGERS`) {
myScore += 20;
}
}
I'm not 100% sure if this is the correct fix since I haven't played around with prompts too often, but lmk if it doesn't work.
Joseph Yhu
PHP Development Techdegree Graduate 48,637 PointsI believe the problem is with this part if (myAngeles.toUpperCase() === 'RAMS' || 'CHARGERS')
. The two conditions separated by the ||
are myAngeles.toUpperCase() === 'RAMS'
and just 'CHARGERS'
, not myAngeles.toUpperCase() === 'CHARGERS'
. And the boolean value of any non-zero value is true, so essentially the condition you put was myAngeles.toUpperCase() === 'RAMS' || TRUE
, which will always be true.
Sergio Morales
455 PointsThanks, Joseph! I see what I did wrong now.
Jordan Laursen
10,465 PointsThank you Joseph for explaining what I was trying to show! I got too caught up in trying to write up the code and forgot to give an explanation. Yours was perfect!
Sergio Morales
455 PointsThanks so much, Jordan! That worked 😃😃😃😃
Sergio Morales
455 PointsOhhh actually the 2nd one was a more concise solution.
Jordan Laursen
10,465 PointsJordan Laursen
10,465 PointsYou may have been using this code inside a function and did not add that part of your code but the || operator should work as I have it above, or you could wrap your answers in parenthesis as well