0

This has got me stumped. I'm creating a simple function that generates a random number between 1 and 6 when an element is clicked (a dice roll).

I want to use an if statement to do something if the number rolled is not a 1 or if the previous number rolled is not a 6, eg:

if(dice !== 1 || lastRoll !== 6) { //Do something

However, I cannot seem to get the OR condition working here. Both dice !== 1 and lastRoll !== 6 work fine independantly, but when combined with the OR condition in one if statement neither work.

My complete function is as such:

var lastRoll = 0;

document.querySelector('.btn-roll').addEventListener('click', function() {

    var dice = Math.floor(Math.random() * 6) + 1;        
    console.log('current ' + dice + ' last ' + lastRoll); //<-- This displays expected results

   if(dice !== 1 || lastRoll !== 6) {
     lastRoll = dice;
     console.log('success');
   }
   else {
     lastRoll = 0;
     console.log('fail');
   }
}

Would anyone know why the OR condition doesn't work?

UPDATE:

To test I did this:

var hardCoded = 6;
if(dice !== 1 || hardCoded !== 6) {

So I expect the condition to always fail. But it doesn't. It still doesn't like the if statement.

4
  • Are you sure you want OR here? Because with if(dice !== 1 || lastRoll !== 6) condition it logs success every time since it 6 passes with the 1st condition and 1 passes with the second condition. I think you need AND here.if(dice !== 1 && lastRoll !== 6) Commented May 16, 2018 at 1:54
  • @Kavindra but I don't want both conditions to be true. I want the condition to fail if they roll a 1 regardless of what the lastRoll is and vice-versa. Basically, if either condition is met it should fail, regardless of what the other variable holds. Commented May 16, 2018 at 1:58
  • Think about your example again. Even if your hardcoded is 6, if the dice is not 1, it doesn't even check for the second condition since it is an OR and the first condition passes. It checks the hardcoded value only if the dice value is 1. You can't expect it to be failed. Commented May 16, 2018 at 2:19
  • Give some example values as of which scenarios should be passed and which scenarios should be failed to understand your requirement properly. Commented May 16, 2018 at 2:42

3 Answers 3

1

From your comment, I guess want you want is if(dice !== 1 && lastRoll !== 6)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I tried that but I don't think it's what I need. This will fail if dice = 1 and lastRoll = 6 simultaneously. Where as I want it to fail if dice = 1 OR if lastRoll = 6 OR if dice = 1 and lastRoll = 6
@MeltingDog The behavior you described you want is exactly what this does with &&'. This answer is logically equivalent to ! ( dice == 1 || lastRoll == 6 ). That's DeMorgan's rule.
0

Make your if statement like below and check, First you need to do something if the roll is not 1:

var lastRoll = 0;

document.querySelector('.btn-roll').addEventListener('click', function() {

  var dice = Math.floor(Math.random() * 6) + 1;        
  console.log('current ' + dice + ' last ' + lastRoll); //<-- This displays expected results

  if(dice !== 1) {
    //dice can be 2,3,4,5,6
    console.log('success');
  }
  else {
    console.log('fail');
  }
  lastRoll = dice;
}

Then you need the last roll not to be 6:

var lastRoll = 0;

document.querySelector('.btn-roll').addEventListener('click', function() {

  var dice = Math.floor(Math.random() * 6) + 1;        
  console.log('current ' + dice + ' last ' + lastRoll); //<-- This displays expected results

  if(dice !== 1) {
    //dice can be 2,3,4,5,6 and lastRoll can be 1,2,3,4,5,6
    if(lastRoll !== 6) {
      //dice can be 2,3,4,5,6 and lastRoll can be 1,2,3,4,5
      console.log('success');
    } else {
      // dice can be 2,3,4,5,6 lastRoll = 6
      console.log('fail');
    }
  }
  else {
    // dice can be 1 lastRoll can be 1,2,3,4,5,6
    console.log('fail');
  }
  lastRoll = dice;
}

Comments

0

@MeltingDog - your code looks fine to me. It's just a very rare occurrence. If you pause the script with debugger here:

        var lastRoll = 0;
        var condition;

        document.querySelector('.btn-roll').addEventListener('click', function() {

            var dice = Math.floor(Math.random() * 6) + 1;        
            console.log('current ' + dice + ' last ' + lastRoll); //<-- This displays expected results

            debugger;
            condition = (dice !== 1 || lastRoll !== 6);
            console.log(condition);

           if(dice !== 1 || lastRoll !== 6) {
             lastRoll = dice;
             console.log('success');
           }
           else {
             lastRoll = 0;
             console.log('fail');
           }
        });

And type dice=1 and lastRoll=6 in the console before resuming, you'll see the "fail" message appear.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.