在javascript中创建随机数组

时间:2017-03-29 07:12:54

标签: javascript arrays

我有一个表格,要求用户在数学练习中获得最大成绩,以便建立一些练习:

<input type="text" id="maxR" placeholder="type maximum result" />
<button id="activate" onclick="makeDrills()">MAKE</button>
<p id="drill"></p>

我希望将所有演习选项的答案翻译为数组。

我需要帮助将这句话翻译成java-script:

&#34;而c> level使用钻头及其结果添加Q的新数组&#34;。

这是我的尝试:

<script>
  function theQ(quest, ans) {  // drills constructor
    this.question = quest;
    this.answer = ans;
  }

  var a, b, c ; // arguments
  var quests = [new theQ()]; // arrays holder
  var level = document.getElementById("maxR").value; // user input

  function makeDrills() { // button pushed
    var c = a+b; // pattern of each drill
    while (c > level) { // as long result smaller then user input
      var a = Math.floor(Math.random() * 10);  // choose random number
      var b = Math.floor(Math.random() * 10); // choose another random number
      quests.push("a+b", "c");  // add the drill to the arrays holder
    }
    document.getElementById("drill").innerHTML += quests; // print drills
  }
</script>

我读过类似的问题并尝试使用他们的技术,但没有成功。

编辑: 此代码用于教育目的:

a,b,c是a + b = c方程的自变量。我尝试制作数学演习生成器,孩子输入最大结果并按下按钮,然后将出现所有可能的演习选项。我需要将所有选项都放在数组中,因为我想将这些选项用作测试。

所以现在我只想了解如何创建这些数组。

2 个答案:

答案 0 :(得分:1)

有几个问题:

  • 创建任务数组时,不应向其添加一个空问题。只需quests = []

  • 在使用 a b 的单独值之前,您正在使用var c = a+b计算总和。 var c = a+b不是一种模式 - 正如你在评论中写的那样 - ;它实际上是在那里执行添加。

  • level 是一个字符串(因为这是输入值总是如此),所以在进行c > level之类的比较时,您要与字符串值进行比较,将无法获得理想的结果。

  • 由于您的金额永远不会超过18( a b 都小于10),如果您输入的话,您的代码将永远运行最大值大于18.此外,将此作为停止条件似乎是不合理的。你甚至可能陷入第一笔金额已超过允许范围的情况,你最终也不会有任何问题。只需要通过询问用户,或者定义代码中的次数(例如:10个问题)来定义要生成的问题数量。

  • 您可以通过查看生成第一个数字后剩余的数量来确保您的金额在限制范围内。如果您的最大值为10,并且您生成了6,那么请将您的第二个数字设为0到4之间的随机数。这样您就可以得到有效的数字。

  • "a+b"作为问题不是很有用,因为用户不知道a的含义。相反,请将其设置为动态,并要求a + ' + ' + b + ' = ',它将呈现为6 + 3 =

  • 同样地,"c"是一个字符串,而不是总和的结果,它应该是c,或者为什么不只是a+b,所以你不需要 c 变量。

  • quests.push("a+b", "c");正在向数组推送两个字符串值,但您想推送一个问题对象,因此您需要使用quests.push(new theQ(a + ' + ' + b + ' = ', a+b))

  • 由于任务是一个对象数组,因此您不应期望将其分配给innerHTML属性将呈现有用的内容。您应该指定如何呈现问题。因此,您需要迭代问题,然后为每个问题生成所需的HTML。

包含所有这些更正的代码如下。它还有一个按钮和代码来验证答案的输入。

function theQ(quest, ans) {  // drills constructor
    this.question = quest;
    this.answer = ans;
}

var quests = []; // array of questions

function makeDrills() { // button pushed
    var a, b; // We don't need c
    // Read the input only when button is pressed:
    var level = document.getElementById("maxR").value; // user input
    // Empty questions array
    quests = [];
    
    // Let's say we want to produce 10 questions
    while (quests.length < 10) { 
        // choose a random integer below the maximum
        a = Math.floor(Math.random() * level);  
        // choose a random integer such that sum is not greater than maximum
        b = Math.floor(Math.random() * (level-a+1));
        // Question is a dynamicly created string, answer is calculated here
        // You need to call theQ here:
        quests.push(new theQ(a + ' + ' + b + ' = ', a+b));
    }

    var container = document.getElementById("drill");
    // Set the HTML for all questions
    container.innerHTML = quests.map(function (quest, i) {
        // Add the question to the drill section, give the input element an ID 
        // that corresponds to the question number
        return quest.question + '<input placeholder="enter sum" id="answer' + i + '"><br>';
    }).join(''); // join all HTML pieces together into one string
}

function verify() {
    if (quests.length == 0) return; // Nothing to do
    // Count the number of wrong answers. Iterate over answers with reduce:
    var errorCount = quests.reduce(function (count, quest, i) {
        // Add 1 penalty if answer is incorrect
        return count + (quest.answer != document.getElementById('answer' + i).value);
    }, 0); // Atart counting from 0
    if (errorCount == 0) 
        alert('Congratulations! You answered all questions correctly.');
    else
        alert('You have entered ' + errorCount + ' wrong answer(s). Try to correct them.');
}
<input type="text" id="maxR" placeholder="type maximum sum" />
<button id="activate" onclick="makeDrills()">Generate questions</button>
<div id="drill"></div>
<button id="verify" onclick="verify()">Verify</button>

答案 1 :(得分:0)

你的代码有点混乱。没有完全理解任务的目的,但首先要更好地组织你的代码。看起来应该是这样的:

function makeDrills() { // button pushed
  var quests = []; // arrays holder
  var level = document.getElementById("maxR").value; // user input
  var a = Math.floor(Math.random() * 10);  // choose random number
  var b = Math.floor(Math.random() * 10); 
  var c = a+b; // pattern of each drill

  while (c < level) { // as long result smaller then user input
    quests.push([a+"+"+b, c]);  // add the drill to the arrays holder
    a = Math.floor(Math.random() * 10);  // choose random number
    b = Math.floor(Math.random() * 10); // choose another random number
    c = a + b
  }
  document.getElementById("drill").innerHTML = quests; // print drills
}
相关问题