javascript - 在我的猜数游戏中遇到 while 循环问题

标签 javascript html loops for-loop while-loop

因此,我无法启动 while 或 for 循环来创建从 6 开始的回合计数,以允许正在猜测数字的用户在他输之前仅转 6 圈,并给出一条消息说明他输了应该被猜到的数字。每次他在文本框中输入一个猜测时,回合计数将一个一个地倒数到 0。我对在哪里创建 while 循环以及在哪里放置 while 循环感到困惑。是否有多种方法可以创建此操作?

谢谢!

    //creating the guessing game function using if and else if statements
var guessGame = function(){
    var guess = document.getElementById("guessTextbox").value;
    var guessOutput = document.getElementById("gameStatus");

    //if guessed number is equal to the random number, winning display message is shown
    if (guess == guessNum){
        guessOutput.value = guessOutput.value + "You've guessed correctly! The correct number was: " + guessNum;
    }
    //else if statement for guesses not in the 1-100 range displaying error/incorrect message
    else if (guess > 100 || guess < 1){
        guessOutput.value = guessOutput.value + "Incorrect number range!";
    }
    else if (guess > guessNum){
        guessOutput.value = guessOutput.value + "The guess is too high!";
    }
    else if (guess < guessNum){
        guessOutput.value = guessOutput.value + "The guess is too low!";
    }
    //else if guess is NOT a number, displays message
    else if (isNaN(guess)){
        guessOutput.value = guessOutput.value + "Input value is not a number!";
    }
};

最佳答案

这是实现该目标的一种方法。我留下了一些评论来帮助您导航。如果我能澄清任何事情,请告诉我。

// When initialized, guessGame returns a function that allows
// 6 guesses and then reveals the random number.
function guessGame() {

    // Start with 6 turns.
    this.turns = 6;

    // Generate random number guessNum s.t. 1 < guessNum < 100
    this.guessNum = Math.floor(Math.random() * (100 - 2)) + 2;

    return function() {
      var guessOutput = document.getElementById("gameStatus");

      // If no turns left, output the message and return.
      if (this.turns === 0) {
        guessOutput.value = guessOutput.value + "You've ran out of turns! The correct number was: " + this.guessNum;
        return;
      }

      // Otherwise, match the next guess against guessNum.
      var guess = parseInt(document.getElementById("guessTextbox").value, 10);

      // If equal, display the winning message
      if (guess === this.guessNum){
          guessOutput.value = guessOutput.value + "You've guessed correctly! The correct number was: " + this.guessNum;
      }
      // If not in range, display error message
      // Advice: might want to move this before the equality check.
      else if (guess > 100 || guess < 1){
          guessOutput.value = guessOutput.value + "Incorrect number range!";
      }
      // If greater than, ...
      else if (guess > this.guessNum){
          guessOutput.value = guessOutput.value + "The guess is too high!";
      }
      // If less than, ...
      else if (guess < this.guessNum){
          guessOutput.value = guessOutput.value + "The guess is too low!";
          }
      // If not a number, display error.
      // Advice: might want to move this to the top of the checks.
      else if (isNaN(guess)){
          guessOutput.value = guessOutput.value + "Input value is not a number!";
      }

      // Decrement the number of turns.
      this.turns -= 1;

    // This helps the embedded function locate the random number.
    }.bind(this);
}

用法。

// Initialize the game
// (Suppose the random number is 30)
var game = new guessGame();

// Enter 21 in guessTextbox
game(); // => "The guess is too low!"

// Enter 22 in guessTextbox
game(); // => "The guess is too low!"

// Enter 23 in guessTextbox
game(); // => "The guess is too low!"

// Enter 24 in guessTextbox
game(); // => "The guess is too low!"

// Enter 50 in guessTextbox
game(); // => "The guess is too high!"

// Enter 49 in guessTextbox
game(); // => "The guess is too high!"

// Enter 48 in guessTextbox
game(); // => "You've ran out of turns! The correct number was: 30"

// Enter 47 in guessTextbox
game();  // => "You've ran out of turns! The correct number was: 30"

你明白了。

关于javascript - 在我的猜数游戏中遇到 while 循环问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33840186/

相关文章:

javascript - Html5/Javascript对接/流程部分布局

html - 是否可以单独使用 CSS 将 "external"HTML 链接设置为与 "relative"链接不同的样式?

javascript - 如何使用 Ramda compose 编写组合函数?

javascript - React-Native 更新 AsyncStorage item

javascript - 发布字符串化数组时格式错误的 JSON(Javascript 和 Ruby)

java - Applet 中的 Paint() 方法内的无限循环不允许我与显示的按钮进行交互

java - 从数组生成变量以创建循环

python - for 循环中的 if/else

javascript - 我如何在javascript中先调用一个函数,然后再调用第二个函数

javascript - 使用速度在使用 processing.js 的 javascript 中移动矩形