我是Java的新手,正在尝试实现重置功能。 这是一款猜数字游戏,猜中正确的数字后,我想通过按重置按钮重置游戏,然后开始新的游戏。
var actualNum = randomNumber();
function randomNumber(){
return Math.floor(Math.random() * 101);
}
function findCorrectGuess(actualNum,userGuess){
if(userGuess > actualNum){
return "That is too high!"
} else if (userGuess < actualNum){
return "That is too low!"
} else if (userGuess === actualNum){
return "BOOM!"
}
}
function reset(){
document.getElementById('input').value = "";
el = document.getElementById('player-guess')
el.removeChild(el.firstChild)
}
function clearField(){
document.getElementById('input').value = "";
}
function guessToUI(){
let userGuess = document.getElementById('input').value;
document.getElementById('player-guess').innerHTML = `<center><h1>Your last guess was</h1><h2>${userGuess}</h2><h3>${findCorrectGuess(parseInt(actualNum),parseInt(userGuess))}</h3></center>`
}
function listeners(){
document.getElementById('guesser').addEventListener('click', guessToUI);
document.getElementById('clear').addEventListener('click', clearField);
document.getElementById('reset').addEventListener('click', reset);
}
listeners();
在我猜出正确的数字并单击重置按钮后,似乎由我的randomNumber函数生成的以前的数字仍然保留在新游戏上。谁能帮我解决这个问题?谢谢!
答案 0 :(得分:1)
将生成您的随机数的代码放入reset方法中,以便获得新的随机数。
function reset(){
actualNum = randomNumber();
document.getElementById('input').value = "";
el = document.getElementById('player-guess')
el.removeChild(el.firstChild)
}