我有一个表单,要求用户在数学练习中获得最大的结果,以便构建一些练习:
<input type="text" id="maxR" placeholder="type maximum result" />
<button id="activate" onclick="makeDrills()">MAKE</button>
<p id="drill"></p>我想把所有的演练选项和它们的答案转换为数组。
我需要帮助把这个句子翻译成java脚本:
“当c> level添加新的theQ数组时,同时使用演练和它的结果”。
这是我的尝试:
<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方程的参数。我试着让数学操练生成器,孩子输入最大的结果,并按下按钮,然后所有可能的演习选项将出现。我需要将所有选项作为数组,因为我希望使用这些选项作为一种测试。
所以现在我只想了解如何创建这些数组。
发布于 2017-03-29 09:08:21
有几个问题:
quests = []var c = a+b计算和。var c = a+b不是一个模式--当您在注释中写入时--它实际上是在那里执行加法。c > level这样的比较时,您是在与字符串值进行比较,而字符串值并不会有期望的结果。"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>
发布于 2017-03-29 07:43:06
你的代码有点乱。没有完全理解任务的目的,但首先要更好地组织代码。应该是这样的:
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
}https://stackoverflow.com/questions/43086730
复制相似问题