gpt4 book ai didi

javascript - 如何将用户输入存储在数组中

转载 作者:行者123 更新时间:2023-11-29 22:02:07 25 4
gpt4 key购买 nike

我正在尝试创建一种数学测验,提示用户在回答下一个问题之前先回答一个问题。在测验结束时,弹出窗口应列出所有问题以及用户对每个问题的答案。我已经准备就绪,但我不知道如何将每个用户的答案存储到一个数组中,以便在弹出窗口中显示这些答案。任何帮助将不胜感激!

HTML:

<body>
<form>
<table id="addProblem" width="150" border="0" cellspacing="0" cellpadding="10">
<tr>
<td>&nbsp;</td>
<td colspan="1" align="right"><input id="carryOver"></td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td colspan="2" align="right" id="firstNum">48</td>
</tr>
<tr>
<td>+</td>
<td colspan="2" align="right" id="secondNum">16</td>
</tr>
<tr>
<td colspan="3"><hr id="sepLine"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td colspan="2" align="right"><input id="userAnswer" type="text"></td>
</tr>
<tr>
<td colspan="3"><input type="button" onclick="submitAnswer()" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>

JavaScript:

// first number in addition problem
var numOne = [48,39,16,43,37,23,44,13,37,28,48,16];

// second number in addition problem
var numTwo = [16,22,25,18,46,49,18,39,25,17,9,28];

// counter variable
var i = 0;

function submitAnswer() {
// validate the answer was a number
var guessed = Number(document.getElementById('userAnswer').value);
var checkAnswer = 'Correct!';

// if the answer is incorrect, change result to Incorrect
if (guessed != numOne[i]+numTwo[i]) {
checkAnswer = 'Incorrect!\nThe answer is'+' '+Number(numOne[i]+numTwo[i])+'.';
}
// will show the string in the result variable
if(confirm(checkAnswer) && i<1) {
// next question
i++;
document.getElementById('firstNum').innerHTML=numOne[i];
document.getElementById('secondNum').innerHTML=numTwo[i];

// reset the answer and carry over to blank
document.getElementById('userAnswer').value = '';
document.getElementById('carryOver').value = '';
} else if(i=1) {
var results=window.open('','name','height=400,width=500');

results.document.write('<html><head><title>Results</title>');
results.document.write('<link rel="stylesheet" href="style.css">');
results.document.write('</head><body>');
results.document.write('1) '+numOne[0]+' + '+numTwo[0]+' = ' + 'user input for Problem 1');
results.document.write('<br>');
results.document.write('2) '+numOne[1]+' + '+numTwo[1]+' = ' + 'user input for Problem 2');
results.document.write('<p><a href="javascript:self.close()">Close</a> the popup.</p>');
results.document.write('</body></html>');
results.document.close();
}
}

最佳答案

var answers = [];
answers.push(document.getElementById('userAnswer').value); //or something else to push

并将 if(i=1) 替换为 if(i==1)

关于javascript - 如何将用户输入存储在数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23179372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com