当我试图向JS中添加一个运算符时,++运算符在JS中不起作用



我知道以前有人问过这个问题。但情况不同。所以我有一个分数变量,当用户回答问题时,我会增加1。它开始工作,但停止工作。我不认为这是后缀或前缀。这是我的代码:

<!DOCTYPE html>
<html>
<head>
<title>
Math 24
</title>
</head>
<body>
<h1>
Play this game by typing a expression that = 24. Do not type the result or the equal sign
</h1>
<p id="d">
</p>
<p id="score">
Your score: 0
</p>
<label for="Enter your expression">
Enter your expression
</label>
<input type="text" id="input">
<button onclick="myResult()">
Check
</button>
<p id="error">
</p>
<button onclick="new_puzzle()">
New Challenge
</button>
<p>
Can't solve the puzzle?
</p>
<button onclick="skip()">
Skip This One
</button>
<script>
function myResult(){
var score = 0;
var getInputValue = document.getElementById('input').value;
if(getInputValue.includes(num1, num2, num3, num4) == true){
parseInt(getInputValue);
eval(getInputValue);
}
if(eval(getInputValue) == 24){
score++;
document.getElementById('error').innerHTML = 'Great Job! You got it!';
document.getElementById('score').innerHTML = 'Your score: ' + score;
}
else if(getInputValue === ""){
document.getElementById('error').innerHTML = 'Empty field are not allowed';
}
else{
document.getElementById('error').innerHTML = 'Keep trying';
}

}
</script>
<script>
var num1 = Math.ceil(Math.random() * 9);
var num2 = Math.ceil(Math.random() * 9);
var num3 = Math.ceil(Math.random() * 9);
var num4 = Math.ceil(Math.random() * 9);
String(num1, num2, num3, num4);
document.getElementById('d').innerHTML = "Your numbers are:" + num1 + ' ' + num2 + ' ' + num3 + ' ' + num4;
</script>
<script>
function new_puzzle(){
var num1 = Math.ceil(Math.random() * 9);
var num2 = Math.ceil(Math.random() * 9);
var num3 = Math.ceil(Math.random() * 9);
var num4 = Math.ceil(Math.random() * 9);
String(num1, num2, num3, num4);
document.getElementById('d').innerHTML = "Your numbers are:" + num1 + ' ' + num2 + ' ' + num3 + ' ' + num4;
document.getElementById('error').style.display === "none";
}
</script>
<script>
function skip(){
var num1 = Math.ceil(Math.random() * 9);
var num2 = Math.ceil(Math.random() * 9);
var num3 = Math.ceil(Math.random() * 9);
var num4 = Math.ceil(Math.random() * 9);
String(num1, num2, num3, num4);
document.getElementById('d').innerHTML = "Your numbers are:" + num1 + ' ' + num2 + ' ' + num3 + ' ' + num4;
document.getElementById('error').style.display === "none";
}
</script>
</body>
</html>

只要问题回答正确,分数就应该增加一分。

每次单击按钮时,都会调用myResult函数,将score重新定义为0。您可以尝试将score作为全局变量,方法是从函数中取出声明。

最新更新