如何在javascript中的括号之间放置重复数字?

时间:2013-12-11 14:01:25

标签: javascript dice

我需要一些练习帮助。它是关于掷骰子(那是放在一个阵列中)40次,当你连续掷出两个相同的数字时,你获得5分。在控制台中它看起来像这样:

You roll the dice, the number is 5.
[6, 4, 2, 2, 1, 5, 4, 4, 2, 1, 2, 3, 3, 1, 4, 3, 5, 5, 1, 1, 5, 5]
You won 5 points!!

现在我想要圆括号之间的重复数字,所以它看起来像这样:

[6, 4, (2, 2), 1, 5, (4, 4), 2, 1, 2, (3, 3), 1, 4, 3, (5, 5), (1, 1), (5, 5)]

到目前为止,这是我的代码:

var diceRolls = [];
    var getallen = [1,2,3,4,5,6];
    var totalDiceRolls = 0;
    var maxDiceRolls = 40;
    var totalPoints = 40;

function rollDice() {
 if (++totalDiceRolls > maxDiceRolls)
   {
       alert(maxDiceRolls + " dice rolls allowed at max!");
       return;
   }

   totalPoints--;

   var d1 = getallen[Math.floor(Math.random() * getallen.length)];
   console.log("You roll the dice, the number is "+d1+".");
      diceRolls.push(d1);
    console.log(diceRolls);



   if(diceRolls[diceRolls.length - 2] === d1) {
       totalPoints+=5;
       console.log("You won 5 points!!");
   } 
   console.log("You now have "+totalPoints+" points left.");
}

0 个答案:

没有答案