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

would this ever print the else condition?

var ships = 10;
var score = 0;
if (  ships === 0 || score === 0 ) {
  console.log('Game over.');
}  else {
  console.log('Your score is zero.');
}

This was in the quiz, but I wondered why the person bothered to write the else condition? if either variable wasn't 0 would it print "your score is 0"? am I missing something?

3 Answers

Hii. Well I don't think it would ever go into else condition as the score variable is set to 0 and we are using or operator. program checks for first condition where ships are === 0 and it returns true so technically it never checks the second condition. If put score === 0 before ships === 0 then it will always go into second condition.

This is what I think I might be wrong as I am a beginner inJavaScript. So If you don't find my answer relevant so I apologies for that.

I would really appreciate your view on this

Himanshu,

you are very close on this, but you missed one thing; ships equal 10 not 0, But you are right. It will never go to the else statement, because of the or operator. What it is saying is does ships equal 0? No ships equals 10, so that is false, but it also asks or is the score 0? and yes the score is 0 so it is true. the great thing about the or operator is you can have either conditions return true, for the condition to be true, where as the and operator has to have all conditions be true. But you are right it will always return true.

Thanks :)

Not a problem.

Happy Coding!

Thank you Jacob and yes I was not right . Thanx for correcting me man :)