如何在javascript数组中存储元素?

时间:2017-12-08 20:03:43

标签: javascript arrays

我正在尝试建立一个纸牌游戏,其中两个玩家获得随机牌,获得最高牌数的人获胜。

我尝试将生成的随机数推入数组。这里遇到的麻烦是新数字应该添加到数组中,但是它会离开数组为空。有人可以帮助我。



 var varCounter = 1;
    var refVar = setInterval(function() {
    
      if (varCounter <= 4) {
    
        var randomnumber = Math.floor(Math.random() * (13 - 1 + 1)) + 1;
        var player1 = [];
        player1.push(randomnumber);
    
        for (var i = 0; i < player1.length; i++) {
          document.write("<br>");
          document.write("player1 " + i + "card");
          document.write("<br>");
          document.write(player1[i]);
    
        }
        varCounter++;
      } else if (varCounter >= 5 && varCounter <= 8) {
        var randomnumber2 = Math.floor(Math.random() * (13 - 1 + 1)) + 1;
        var player2 = [];
        player2.push(randomnumber2);
        for (var i = 0; i < player2.length; i++) {
          document.write("<br>");
          document.write("player2 " + i + "card");
          document.write("<br>");
          document.write(player2[i]);
        }
        varCounter++;
      } else {
        clearInterval(refVar);
      }
    }, 100);
&#13;
&#13;
&#13;

预期输出:

player1 1card
9
player1 2card
12
player1 3card
4
player1 4card
12
player2 1card
5
player2 2card
7
player2 3card
9
player2 4card
2 

CURRENT OUTPUT:

player1 0card
9
player1 0card
12
player1 0card
4
player1 0card
12
player2 0card
5
player2 0card
7
player2 0card
9
player2 0card
2

3 个答案:

答案 0 :(得分:1)

正如您对问题的评论所指出的那样,如果您想在每次迭代中编写内容,那么每次遍历计数器时,您的数组player1player2都会被覆盖,因为它包含在迭代中,我建议像这样取出for循环:

if (varCounter <= 4) {
  var randomnumber = Math.floor(Math.random() * (13 - 1 + 1)) + 1;
  var player1 = [];
  player1.push(randomnumber);

  document.write("<br>");
  document.write("player1 " + varCounter + "card"); // varCounter becomes your 1 2 3 4card
  document.write("<br>");
  document.write(player1);

  varCounter++;
}

或者像这样探索while循环:https://codepen.io/corviday/pen/pdMyEw?editors=1010

希望这有帮助!

答案 1 :(得分:1)

这是我最终提出的。

var varCounter = 0;
var player1 = [];
var player2 = [];
var refVar = setInterval(function() {
  if (varCounter <= 3) {
    var randomnumber = Math.floor(Math.random() * (13 - 1 + 1)) + 1;

    player1.push(randomnumber);
    var i = varCounter % 4;
    for (i; i < player1.length; i++) {

      document.write("<br>");
      document.write("player1 " + (i + 1) + "card");
      document.write("<br>");
      document.write(player1[i]);

    }
    varCounter++;
  } else if (varCounter >= 4 && varCounter <= 7) {
    var randomnumber2 = Math.floor(Math.random() * (13 - 1 + 1)) + 1;

    player2.push(randomnumber2);
    var i = varCounter % 4;
    for (i; i < player2.length; i++) {

      document.write("<br>");
      document.write("player2 " + (i + 1) + "card");
      document.write("<br>");
      document.write(player2[i]);

    }
    varCounter++;
  } else {
    clearInterval(refVar);
  }
}, 100)

答案 2 :(得分:0)

如果您只想要所需的输出,请尝试此操作。我更改了varCounter相关代码,因此可以很好地使用varCounter%并使其成为可运行的代码段。

var varCounter = 0;
var refVar = setInterval(function() {

  if (varCounter < 4) {
    var randomnumber = Math.floor(Math.random() * (13 - 1 + 1)) + 1;
    var player1 = [];
    player1.push(randomnumber);

    for (var i = 0; i < player1.length; i++) {
      document.write("<br>");
      document.write("player1 " + (varCounter%4+1) + "card");
      document.write("<br>");
      document.write(player1[i]);

    }
    varCounter++;
  } else if (varCounter >= 4 && varCounter < 8) {
    var randomnumber2 = Math.floor(Math.random() * (13 - 1 + 1)) + 1;
    var player2 = [];
    player2.push(randomnumber2);
    for (var i = 0; i < player2.length; i++) {
      document.write("<br>");
      document.write("player2 " + (varCounter%4+1) + "card");
      document.write("<br>");
      document.write(player2[i]);
    }
    varCounter++;
  } else {
    clearInterval(refVar);
  }
}, 100);

或者,如果你真的想要用卡值填充数组的结果,(编辑:删除了for循环,再次添加for循环来检查数组内容)

var varCounter = 0;

var player1 = [];
var player2 = [];

var refVar = setInterval(function() {

  if (varCounter < 4) {
    var randomnumber = Math.floor(Math.random() * (13 - 1 + 1)) + 1;
    player1.push(randomnumber);

    document.write("<br>");
    document.write("player1 " + (varCounter%4+1) + "card");
    document.write("<br>");
    document.write(player1[varCounter%4] + " "); //Removed for loop

    varCounter++;
  } else if (varCounter >= 4 && varCounter < 8) {
    var randomnumber2 = Math.floor(Math.random() * (13 - 1 + 1)) + 1;
    player2.push(randomnumber2);

    document.write("<br>");
    document.write("player2 " + (varCounter%4+1) + "card");
    document.write("<br>");
    document.write(player2[varCounter%4] + " "); //Removed for loop

    varCounter++;
  } else {
    clearInterval(refVar);
  }
}, 100);

编辑: OP的最终答案:

var varCounter = 0;
var player1 = [];
var player2 = [];
var refVar = setInterval(function() {
  if (varCounter <= 3) {
    var randomnumber = Math.floor(Math.random() * (13 - 1 + 1)) + 1;

    player1.push(randomnumber);
    var i = varCounter % 4;
    for (i; i < player1.length; i++) {

      document.write("<br>");
      document.write("player1 " + (i + 1) + "card");
      document.write("<br>");
      document.write(player1[i]);

    }
    varCounter++;
  } else if (varCounter >= 4 && varCounter <= 7) {
    var randomnumber2 = Math.floor(Math.random() * (13 - 1 + 1)) + 1;

    player2.push(randomnumber2);
    var i = varCounter % 4;
    for (i; i < player2.length; i++) {

      document.write("<br>");
      document.write("player2 " + (i + 1) + "card");
      document.write("<br>");
      document.write(player2[i]);

    }
    varCounter++;
  } else {
    clearInterval(refVar);
  }
}, 100)