为了账号安全,请及时绑定邮箱和手机立即绑定

猜数字游戏:进行循环/警报+重置

猜数字游戏:进行循环/警报+重置

守着一只汪 2021-09-30 17:58:45
我想创建一个“猜数字游戏”,您可以在一定数量的尝试中猜出一个随机数。我挣扎于:(必须有) 1. 在警报之后循环进行(显示您的猜测是过高、过低还是不是数字。(很高兴拥有) 2. 如果您设法猜到数字或尝试次数用完,请重置游戏。重置意味着恢复原始的猜测数量并将您的尝试归零。也许只是一个刷新页面的命令。我 - 在我第一次尝试后休息多年之后 - 编码新手。这是对我的项目的访问:https : //glitch.com/~gravel-pyrite我认为这是我搞砸的相关循环/条件引擎:var rangeMin = 0; //set the min value var rangeMax = 10; //set the max value var randomNumber = Math.floor(Math.random() * (rangeMax - rangeMin + 1)) + rangeMin;var counter = 0;var maxTries = 5;var remainingTries = maxTries;var guessedNumbers = [];document.getElementById("submit").onclick = function() {//Displaying min, max and remaining guesses in HTML  document.getElementById('min').innerHTML = rangeMin;  document.getElementById('max').innerHTML = rangeMax+"!";  document.getElementById('feedback').innerHTML = "Remaining guesses: "+remainingTries;  document.getElementById('guessedNumbers').innerHTML = "Your guesses so far: " + guessedNumbers;  var attempts = +document.getElementById("guessInput").value; // use `+` to make sure your input value is treated as a number and not a string  guessedNumbers.push(" "+attempts+" ");  counter++;  remainingTries--;  if (counter >= maxTries) { //If running outs of attempts, break out of loop (test for >= to)    alert("You used up your maximum tries. A new game will start.");    resetGame();  } else if (isNaN(attempts)) { //If anything else than a number is entered    alert(attempts + " is not a number");  } else if (attempts == randomNumber) { //If guessed correctly    alert("Awesome! You guessed the number right: " + randomNumber + ". You needed " + attempts + " attempts.");    resetGame();  } else if (attempts > randomNumber && counter < maxTries) {    alert("Your guess " + attempts + " is too high. Guess lower.")  } else if (attempts < randomNumber && counter < maxTries) {    alert("Your guess " + attempts + " is too low. Guess higehr.")  }}function resetGame() {  location.reload(true);}我希望在警报之后,我可以输入一个新值,而不是陷入“警报循环”。
查看完整描述

3 回答

?
红颜莎娜

TA贡献1842条经验 获得超12个赞

这是我的结果。我确信事情可以改进,但一切正常:


var rangeMin = 0; //set the min value 

var rangeMax = 10; //set the max value 

var randomNumber = Math.floor(Math.random() * (rangeMax - rangeMin + 1)) + rangeMin;

var counter = 0;

var maxTries = 5;

var remainingTries = maxTries;

var guessedNumbers = [];


//Displaying min, max and remaining guesses in HTML

document.getElementById('min').innerHTML = rangeMin;

document.getElementById('max').innerHTML = rangeMax+"!";

document.getElementById('feedback').innerHTML = "Remainig guesses: "+remainingTries;

document.getElementById('guessedNumbers').innerHTML = "Your guesses so far: " + guessedNumbers;


//The code

document.getElementById("submit").onclick = function() {



  var attempts = +document.getElementById("guessInput").value; // use `+` to make sure your input value is treated as a number and not a string

  guessedNumbers.push(" "+attempts+" ");

  counter++;

  remainingTries--;

  if (counter >= maxTries) { //If running outs of attempts, break out of loop (test for >= to)

    document.getElementById('feedback').innerHTML = "Remainig guesses: "+remainingTries;

    document.getElementById('guessedNumbers').innerHTML = "Your guesses so far: " + guessedNumbers;alert("You used up your maximum tries. A new game will start.");

    resetGame();

  } else if (isNaN(attempts)) { //If anything else than a number is entered

    document.getElementById('feedback').innerHTML = "Remainig guesses: "+remainingTries;

    document.getElementById('guessedNumbers').innerHTML = "Your guesses so far: " + guessedNumbers;

    alert(attempts + " is not a number");

  } else if (attempts == randomNumber) { //If guessed correctly

    document.getElementById('feedback').innerHTML = "Remainig guesses: "+remainingTries;

    document.getElementById('guessedNumbers').innerHTML = "Your guesses so far: " + guessedNumbers;

    alert("Awesome! You guessed the number right: " + randomNumber + ". You needed " + attempts + " attempts.");

    resetGame();

  } else if (attempts > randomNumber && counter < maxTries) {

    document.getElementById('feedback').innerHTML = "Remainig guesses: "+remainingTries;

    document.getElementById('guessedNumbers').innerHTML = "Your guesses so far: " + guessedNumbers;

    alert("Your guess " + attempts + " is too high. Guess lower.")

  } else if (attempts < randomNumber && counter < maxTries) {

    document.getElementById('feedback').innerHTML = "Remainig guesses: "+remainingTries;

    document.getElementById('guessedNumbers').innerHTML = "Your guesses so far: " + guessedNumbers;

    alert("Your guess " + attempts + " is too low. Guess higehr.")

  }

}


function resetGame() {

  location.reload(true);

}



查看完整回答
反对 回复 2021-09-30
  • 3 回答
  • 1 关注
  • 260 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信