저는 Javascript에서 아주 새로운데, 간단한 숫자 추측 게임을 만들었습니다. 이 프로그램은 1-10 사이의 난수를 생성하고 추측이 너무 높거나 낮을 때 알려줍니다. 네가 맞히면, 네가 이긴다.Javascript 번호 추측 게임에 추측 루프 추가
플레이어에게 3 가지 추측을 제공하여이 현재 버전을 개선하고 싶습니다. 그 후에 플레이어가이기거나 잃게됩니다. 나는 이것이 루프로 끝난다는 것을 알고 있지만, 나는 내 삶을 위해 어떻게 할 수 있는지 알 수 없다. While 루프를 사용해 보았지만 작동하지 않습니다. 여기에 내 코드이며, 당신의 도움에 대해 감사 : 그래서 여기
<script>
var rannum = Math.floor((Math.random() * 10) + 1); //generates a random number and stores it in the rannum variable
function submitanswer(){ //This function analyzes player response and is called by the submit button
var playerguess = document.getElementById("guess"); //grabs whatever the player types in and stores it in the playerguess variable
if (playerguess.value == rannum){ //if the value of playerguess is the same as rannum then the player wins
alert("You win!");
location.reload();
}
if (playerguess.value > rannum){ //if the player's guess is higher than the random number alert too high
alert("Too high!");
document.getElementById("guess").value='';
}
else if (playerguess.value < rannum){ //if the player's guess is lower than the random number alert too low
alert("Too low!");
document.getElementById("guess").value='';
}
}
function reloaD(){ //start over by generating a new number
document.getElementById("guess").value='';
location.reload();
}
</script>
</body>
</html>
루프를 사용하지 마십시오. 사용자가 단어 *를 입력 한 후 * 버튼을 누르거나 Enter 키를 누르는 등의 작업을 수행 할 때까지 기다립니다. 그런 다음 논리와 함께 * 계속 : 추측이 맞습니까? 그렇지 않다면, 그들은 너무 많은 시간을 잘못 추측 했습니까? 그렇다면 GAME OVER. – user2864740
처음에 다시 불러올 필요가 없도록 호출 할 수있는 초기화 기능을 생성하십시오. 루프를 사용할 수 없습니다. 카운터를 3으로 설정하고 초기화가 0 일 때 init을 호출 할 수 없습니다. 초기화에는 생성기와 카운터가 재설정됩니다. – mplungjan