2
저는 JavaScript가 새로 도입되었으므로 맨 처음 나와주세요! 나는 다음 질문으로 이동하기 전에 하나의 질문에 대답하도록 사용자에게 묻는 수학 퀴즈를 만들기 위해 노력하고 있습니다. 퀴즈가 끝나면 팝업 창에 모든 문제와 각 문제에 대한 사용자의 답변이 나열되어 있어야합니다. 나는 거의 자리를 잡았지만, 팝업 창에 이러한 대답을 표시하기 위해 각 사용자의 답변을 배열에 저장하는 방법을 알아낼 수 없습니다. 어떤 도움이라도 대단히 감사하겠습니다!자바 스크립트 : 사용자 입력을 배열에 저장하십시오.
HTML :
<body>
<form>
<table id="addProblem" width="150" border="0" cellspacing="0" cellpadding="10">
<tr>
<td> </td>
<td colspan="1" align="right"><input id="carryOver"></td>
<td> </td>
</tr>
<tr>
<td> </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> </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>
자바 스크립트 :
// 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();
}
}
로
if(i=1)
교체 = 1)? – ryanlutgen