0

I am trying to understand why the below code does not work even though a variation of it works. My understanding eventually both mean the same thing.(I am pretty sure I am wrong, but don't know why)

this works

while (!(luc === 'rock' || luc === 'paper' || luc === 'scissor')) {

this does not work

while (luc !== 'rock' || luc !== 'paper' || luc !== 'scissor')) {

Here is complete context

var uc = prompt('Choose between Rock, Paper and Scissor')
var luc = uc.toLowerCase();
while (!(luc === 'rock' || luc === 'paper' || luc === 'scissor')) {
  var uc = prompt('You did not select between rock paper and scissor')
  var luc = uc.toLowerCase();
}
console.log(uc)
Renan Araújo
  • 3,533
  • 11
  • 39
  • 49
Taha Sk
  • 67
  • 1
  • 7
  • its `!=` not `!==`. Try `!=` it will work. AND you have an extra `)` at the end of the while statement. Do this `while (luc != 'rock' || luc != 'paper' || luc != 'scissor')` – hulkinBrain Jun 21 '16 at 17:30
  • 3
    This might help you understand the logic of the statements https://en.wikipedia.org/wiki/De_Morgan%27s_laws – Syafiq Kamarul Azman Jun 21 '16 at 17:31

1 Answers1

0

the or operator (||) looks if either condition is truthy. So the case that works for you is looking for whether the user input is either rock, paper, or scissors and then negates that value.

The second case looks for example if the input is not rock. Lets assume that the input is scissor (valid input) but the check for is not rock will true and thus the entire or chain will evaluate to true due to at least one truthy value present.

zwwatts
  • 66
  • 5