另一个骰子错误,代码不太正常

时间:2014-05-09 06:16:47

标签: javascript probability dice

第6行说它期待一个标识符。 我试图让它在任一个模具上滚动1使得得分为0,滚动双打使你的得分加倍,否则,得分是两个死亡的总和。

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;
else {
    score = die1 + die2;
}
  if(die1 === die2)
    score = 2*(die1 +die2);
  else{
    score = die1 + die2;
}
}
console.log("You rolled a "+die1+" and a "+die2+" for a score of "+score);

2 个答案:

答案 0 :(得分:1)

您从未在“其他”之前关闭括号。

更改

if(die1 === 1 || die2 === 1){
    score = 0;
else {
    score = die1 + die2;
}

if(die1 === 1 || die2 === 1){
    score = 0;
} else {
    score = die1 + die2;
}

答案 1 :(得分:0)

这是您的代码格式正确并且清除了if语句:

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;
else if (die1 === die2)
    score = 2 * (die1 + die2);
else
    score = die1 + die2;

console.log("You rolled a " + die1 + " and a " + die2 + " for a score of " + score);