Reputation: 11
Im doing this for a dice game where the player is switched if either dice rolled is a 1 or if 6 is rolled twice in a row. My friends code worked but mine didn't, It looks like my if statement accomplishes the same thing.
This code works(friends code):
if (dice === 1 || diceTwo === 1) {
nextPlayer();
} else if (doubleSix === 6) {
if (dice === 6 || diceTwo === 6) {
roundScore = 0;
score[activePlayer] = 0;
nextPlayer();
}
} else {
roundScore += (dice + diceTwo);
document.querySelector('#current-' + activePlayer).textContent = roundScore;
doubleSix = dice;
}
This code does not (my code):
if (dice !== 1 || diceTwo !== 1) {
//Add score
if (doubleSix === 6 && dice === 6) {
roundScore = 0;
score = 0;
nextPlayer();
} else if {
roundScore += (dice + diceTwo) ;
document.querySelector('#current-' + activePlayer).textContent = roundScore;
doubleSix = dice;
}
} else {
//Next Player
nextPlayer();
}
Upvotes: 1
Views: 59
Reputation: 5041
Firstly your friends code only requires one dice to be a 1. You require both dice to be a 1 to run nextPlayer
This is because of what is called De Morgan's laws
Your code should have
if (dice !== 1 && diceTwo !== 1) {
Suggested Improvements..
As a general rule it is a bad idea to call items of a similar nature dice
, diceTwo
etc. It is much better to have an array of dice
, as if you increase the number of dice, the code still works without modification.
Also, I am not sure why you are only looking for a six with the first dice of the previous round, joined with any dice of the current round. I would have thought you were looking for any six in the previous round with any six in the current round...
Your friends code would be better as...
var foundSix = false;
// method to sum array
function sum(total, num) {
return total + num;
}
// ... more code
// check for a 1...
if (dice.indexOf(1) >= 0) {
nextPlayer();
} else if (foundSix) {
// check for a 6
if (dice.indexOf(6) >= 0) {
roundScore = 0;
score[activePlayer] = 0;
nextPlayer();
}
} else {
// roundScore += (dice[0] + dice[1]);
// use array reduce here...
roundScore = dice.reduce( sum, roundScore);
document.querySelector('#current-' + activePlayer).textContent = roundScore;
// doubleSix = dice[0];
// check ALL dice for a six..
foundSix = (dice.indexOf(6) >= 0);
}
Upvotes: 3
Reputation: 1347
Just a quick look, but shouldn't this: (dice !== 1 || diceTwo !== 1) be changed to an AND like this: (dice !== 1 && diceTwo !== 1) because you are checking that a one was not rolled by both dice and diceTwo?
Upvotes: 0
Reputation: 21475
Your if statements do not do the same thing.
Your friend's code:
if (dice === 1 || diceTwo === 1) {
// dice is 1, or diceTwo is 1.
} else {
// neither dice nor diceTwo is 1.
}
Your code:
if (dice !== 1 || diceTwo !== 1) {
// dice is not 1, or dice2 is not 1. In other words this will match all cases except snake eyes.
} else {
// both dice and diceTwo === 1
}
Upvotes: 0
Reputation: 181715
Read up on De Morgan's rule. The negation of dice === 1 || diceTwo === 1
is not dice !== 1 || diceTwo !== 1
, but rather dice !== 1 && diceTwo !== 1
.
In words: the opposite of "one of the dice is 1" is not "one of the dice is not 1", but rather "both of the dice are not 1".
Upvotes: 1