随机数组更新

时间:2019-07-08 06:41:58

标签: javascript jquery html arrays

我有一个数组,其中两个值以随机顺序进行比较。如何更新值?帮助,如果不是很难的话。

$(document).ready(function() {
    var myArray = [{
      "q1": "a",
      "an": "a"
    }, {
      "q1": "b",
      "an": "b"
    }, {
      "q1": "c",
      "an": "c"
    }];
    var rand = Math.floor(Math.random() * myArray.length);
    $("#question").html("<div id='text'>" + myArray[rand].q1 + "</div>");
    $("#btn").click(function() {
      var answer = $("#answer").val();
      if (answer == myArray[rand].an) {
        $("#comment").html("<div>" + "OK!" + "</div>");
      } else {
        $("#comment").html("<div>" + "NO!" + "</div>");
      }
      $("#answer").val('');
    });
  });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="wrap">
    <div id="question"></div>
    <input id="answer" />
    <div id="comment"></div>
    <button id="btn">Button</button>
  </div>

4 个答案:

答案 0 :(得分:1)

<div id="wrap">
    <div id="question"></div>
    <input id="answer" />
    <div id="comment"></div>
    <button id="btn">Button</button>
    <button id="next">Next</button>
  </div>

JS

$(document).ready(function() {
    var myArray = [{
      "q1": "a",
      "an": "a"
    }, {
      "q1": "b",
      "an": "b"
    }, {
      "q1": "c",
      "an": "c"
    }];
    var rand = Math.floor(Math.random() * myArray.length);
    $("#question").html("<div id='text'>" + myArray[rand].q1 + "</div>");
    $("#btn").click(function() {
      var answer = $("#answer").val();
      if (answer == myArray[rand].an) {
        $("#comment").html("<div>" + "OK!" + "</div>");
      } else {
        $("#comment").html("<div>" + "NO!" + "</div>");
      }
      $("#answer").val('');
    });
  $("#next").click(function(){

   var newRand =rand;
   while(rand==newRand)
   {
     newRand = Math.floor(Math.random() * myArray.length);
   }
rand =newRand;
    $("#question").html("<div id='text'>" + myArray[newRand].q1 + "</div>");
  });
});

这是小提琴 https://jsfiddle.net/eax8cvwn/

答案 1 :(得分:0)

您可以像这样访问数组值:

myArray[0].an = 'new value';

0是数组中的“行”。

答案 2 :(得分:0)

点击按钮后,您需要重置随机值。因此,将其置于按钮点击下方的行中。

rand = Math.floor(Math.random() * myArray.length);
$("#question").html("<div id='text'>" + myArray[rand].q1 + "</div>");

$(document).ready(function() {
  var myArray = [{
    "q1": "a",
    "an": "a"
  }, {
    "q1": "b",
    "an": "b"
  }, 
  {
    "q1": "c",
    "an": "c"
  },
  {
    "q1": "d",
    "an": "d"
  },
  {
    "q1": "e",
    "an": "e"
  }];
  var rand = Math.floor(Math.random() * myArray.length);
  $("#question").html("<div id='text'>" + myArray[rand].q1 + "</div>");
  $("#btn").click(function() {
    var answer = $("#answer").val();
    if (answer == myArray[rand].an) {
      $("#comment").html("<div>" + "OK!" + "</div>");
    } else {
      $("#comment").html("<div>" + "NO!" + "</div>");
    }
    $("#answer").val('');
    rand = Math.floor(Math.random() * myArray.length);
    $("#question").html("<div id='text'>" + myArray[rand].q1 + "</div>");
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="wrap">
  <div id="question"></div>
  <input id="answer" />
  <div id="comment"></div>
  <button id="btn">Button</button>
</div>

答案 3 :(得分:0)

将新生成的随机数推入数组,并使用indexOf检查它是否存在并生成新的随机数。并检查是否所有问题都使用问题的数组长度和随机数数组长度回答,然后完成测验。

尝试这样

$(document).ready(function() {
    var myArray = [{
      "q1": "a",
      "an": "a"
    }, {
      "q1": "b",
      "an": "b"
    }, {
      "q1": "c",
      "an": "c"
    }];
    var rand = Math.floor(Math.random() * myArray.length);
    $("#question").html("<div id='text'>" + myArray[rand].q1 + "</div>");
    var askedQtn = [];
    askedQtn.push(rand);
    $("#btn").click(function() {
      var answer = $("#answer").val();
      if (answer == myArray[rand].an) {
        $("#comment").html("<div>" + "OK!" + "</div>");
      } else {
        $("#comment").html("<div>" + "NO!" + "</div>");
      }
      $("#answer").val('');
      var newRand =rand;
      if(myArray.length <= askedQtn.length){
        $("#wrap").html("<div id='text'>Questions Over</div>");
      } else {
        while(askedQtn.indexOf(rand) >= 0)
        {
          rand = Math.floor(Math.random() * myArray.length);
        }
        askedQtn.push(rand);
        $("#question").html("<div id='text'>" + myArray[rand].q1 + "</div>");
       }
     
    });
  });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="wrap">
    <div id="question"></div>
    <input id="answer" />
    <div id="comment"></div>
    <button id="btn">Button</button>
  </div>