私はJavascriptを習得しようとしていて、簡単な数学問題に問題を抱えており、正解と誤答にスコアを付ける関数を作ろうとしていましたが、情報を渡す方法を理解できません永久にスコアを保持する私の機能の。誰でも助けてくれますか? Javascript Local対グローバル変数
<html>
<body>
<p id="question"></p>
<input id="answerBox" type=number>
<button onclick="checkAnswer()">Answer</button>
<p id="answer"></p>
<p id="score"></p>
<p id="newQuestion"></p>
<script>
//create MathAddition Problem;
var mathAdd = function(x, y) {
return x + " + " + y + " = "
}
//set random numbers;
var x = Math.floor((Math.random()*10)+1);
var y = Math.floor((Math.random()*10)+1);
var z = x + y
console.log(z)
//Call mathAdd function;
document.getElementById("question").innerHTML=mathAdd(x, y);
//Check Answer
var checkAnswer = function() {
var answer=document.getElementById("answerBox").value;
if(answer == z) {
document.getElementById("answer").innerHTML=answer + " is correct";
document.getElementById("score").innerHTML="Your score is improving" ;
}
else {
document.getElementById("answer").innerHTML=answer + " is not correct";
document.getElementById("score").innerHTML="Your score is dropping. Work harder!" ;
}
document.getElementById("newQuestion").innerHTML="<a href='test7.html'>Next Question</a>"
}
</script>
</body>
</html>
希望の出力を記述してください。 –