First off, you don't set score
to 0. Change score === 0
to score = 0
.
Second, your second if
is still evaluated. Try making it an else if
:
if (die1 === 1 || die2 === 1){
score = 0;
}
else if ...
题
So I'm making a dice game where if either ofd the dice roll a 1, the score is 1, but I can't seem to make that work. I believe everything else it ok.
var die1 = Math.floor(Math.random()*6 + 1);
var die2 = Math.floor(Math.random()*6 + 1);
var score;
if (die1 === 1 || die2 === 1){
score === 0;
}
if (die1 !== 1 || die2 !== 1){
score = die1 + die2;
}
console.log("You rolled a "+die1+" and a "+die2+" for a score of "+score);
解决方案
First off, you don't set score
to 0. Change score === 0
to score = 0
.
Second, your second if
is still evaluated. Try making it an else if
:
if (die1 === 1 || die2 === 1){
score = 0;
}
else if ...
其他提示
Tying together all of the great suggestions here:
if (die1 === 1 || die2 === 1) {
score = 1; // "if either of the dice roll a 1, the score is 1"
// Also, use = for assignment, not == or ===
}
else { // No need to check the inverse - just use else
score = die1 + die2;
}
if (die1 === 1 || die2 === 1) {
score = 1; // You are not comparing you are assigning here
}
else if ... // Better use else if than using if as in your code
For more information :-