我是JavaScript的新手,我正在做一个上课的项目。到目前为止,还没有任何问题,直到出现此问题为止,在我输入输入内容时,什么都不会打印。当我输入问题时,根本没有任何变化。这个问题只是应该创建一个随机数,然后查看用户是否正确猜到了。
<script>
function playGame() {
var answer;
var x = Math.floor((Math.random() * 100) + 1);
var name = document.getElementById('name').value;
var guess = document.getElementById('guess').value;
if (x > guess) {
var answer = "Guess is too low!");
}
else if (x < guess) {
var answer = "Guess is too high!");
}
else if (x == guess) {
var answer = "You guessed correctly!");
}
document.getElementById('final').innerHTML = "Answer: " + answer;
</script>
<h2>Random Number Game by </h2>
<p>Enter Name:
<input id="name" type="text">
</p>
<p>Enter Guess: <input id="guess" type="number" min="1" max="100" onchange="playGame()"></p>
<h2 id="final"></h2>
答案 0 :(得分:1)
代码中存在语法错误。在)
语句中删除结尾的右括号if..else
。 document.getElementById('final').innerHTML = "Answer: " + answer;
应该在函数内。 var answer;
只需要在脚本开头声明一次。
在{{1}之外将x
定义为undefined
,如果playGame
则将x
设置为undefined
并将x == guess
设置为{ {1}}。
x
答案 1 :(得分:1)
您的代码有一些问题:
undefined
结束它来关闭函数<script>
let x;
function playGame() {
var answer;
if (!x) {
x = Math.floor((Math.random() * 100) + 1)
};
var name = document.getElementById('name').value;
var guess = document.getElementById('guess').value;
if (x > guess) {
answer = "Guess is too low!";
} else if (x < guess) {
answer = "Guess is too high!";
} else if (x == guess) {
answer = "You guessed correctly!";
x = void 0;
}
document.getElementById('final').innerHTML = "Answer: " + answer;
}
</script>
<h2>Random Number Game by </h2>
<p>Enter Name:
<input id="name" type="text">
</p>
<p>Enter Guess: <input id="guess" type="number" min="1" max="100" onchange="playGame()"></p>
<h2 id="final"></h2>
声明的末尾删除}
)
移到answer
函数之外。这样var x = Math.floor((Math.random() * 100) + 1);
只会设置一次,而不是每次输入新的猜测值playGame()
重新初始化x
变量。正如您已经声明的那样,您可以通过引用answer
请参见下面的工作示例:
var
答案 2 :(得分:0)
以下内容对我来说似乎是错误的-
if (x > guess) {
var answer = "Guess is too low!");
}
else if (x < guess) {
var answer = "Guess is too high!");
}
else if (x == guess) {
var answer = "You guessed correctly!");
}
为什么在每个语句的末尾有额外的)?您能否尝试在第一个answer
之外定义变量if
,然后为其赋值?
答案 3 :(得分:0)
innerHTML
语句应位于函数内部。oninput
事件代替onchange
,因为当从任何源输入任何输入时,它将触发处理程序功能。
function playGame() {
var answer;
var x = Math.floor((Math.random() * 100) + 1);
var name = document.getElementById('name').value;
var guess = document.getElementById('guess').value;
if (x > guess) {
var answer = "Guess is too low!";
} else if (x < guess) {
var answer = "Guess is too high!";
} else if (x == guess) {
var answer = "You guessed correctly!";
}
document.getElementById('final').innerHTML = "Answer: " + answer;
}
<h2>Random Number Game by </h2>
<p>Enter Name:
<input id="name" type="text">
</p>
<p>Enter Guess: <input id="guess" type="number" min="1" max="100" oninput="playGame()"></p>
<h2 id="final"></h2>
答案 4 :(得分:0)
它不起作用的原因是因为您没有该函数的右括号,并且在定义answer
的每一行上都有多余的括号:
function playGame() {
var answer;
var x = Math.floor((Math.random() * 100) + 1);
var name = document.getElementById('name').value;
var guess = document.getElementById('guess').value;
if (x > guess) {
var answer = "Guess is too low!";
}
else if (x < guess) {
var answer = "Guess is too high!";
}
else if (x == guess) {
var answer = "You guessed correctly!";
}
document.getElementById('final').innerHTML = "Answer: " + answer;
}
<h2>Random Number Game by </h2>
<p>Enter Name:
<input id="name" type="text">
</p>
<p>Enter Guess: <input id="guess" type="number" min="1" max="100" onchange="playGame()"></p>
<h2 id="final"></h2>