首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在javascript中创建随机数组

在javascript中创建随机数组
EN

Stack Overflow用户
提问于 2017-03-29 07:12:54
回答 2查看 119关注 0票数 0

我有一个表单,要求用户在数学练习中获得最大的结果,以便构建一些练习:

代码语言:javascript
复制
<input type="text" id="maxR" placeholder="type maximum result" />
<button id="activate" onclick="makeDrills()">MAKE</button>
<p id="drill"></p>

我想把所有的演练选项和它们的答案转换为数组。

我需要帮助把这个句子翻译成java脚本:

“当c> level添加新的theQ数组时,同时使用演练和它的结果”。

这是我的尝试:

代码语言:javascript
复制
<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方程的参数。我试着让数学操练生成器,孩子输入最大的结果,并按下按钮,然后所有可能的演习选项将出现。我需要将所有选项作为数组,因为我希望使用这些选项作为一种测试。

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-03-29 09:08:21

有几个问题:

  • 创建任务数组时,不应向其添加一个空问题。只需做quests = []
  • 在得到a和b的单独值之前,您正在用var c = a+b计算和。var c = a+b不是一个模式--当您在注释中写入时--它实际上是在那里执行加法。
  • 级别是一个字符串(因为输入值总是这样的),所以当您进行像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片段。

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

代码语言:javascript
复制
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.');
}
代码语言:javascript
复制
<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
EN

Stack Overflow用户

发布于 2017-03-29 07:43:06

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

代码语言:javascript
复制
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
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43086730

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档