简体   繁体   English

为什么这两个If陈述不相同?

[英]Why are these two If-statements not the same?

I just started learning Javascript. 我刚刚开始学习Javascript。 Currently I'm taking an online course and I just ran into my first problem I do not understand. 目前,我正在上在线课程,遇到了我不明白的第一个问题。

I want to check how many guesses I still have in a Hangman game: 我想检查在Hangman游戏中我还有多少猜测:

const Hangman = function(word, remainingGuesses) {
  this.word = word.toLowerCase().split('')
  this.remainingGuesses = remainingGuesses
  this.guessedLetters = []
}

Hangman.prototype.getPuzzle = function() {
  let puzzle = ''

  this.word.forEach((letter) => {
    if (this.guessedLetters.includes(letter) || letter === ' ') {
      puzzle += letter
    } else {
      puzzle += '*'
    }
  })

  return puzzle
}

the correct if statement in the video is this: 视频中正确的if语句是这样的:

Hangman.prototype.makeGuess = function(guess) {

  const isUnique = !this.guessedLetters.includes(guess)
  const isBadGuess = !this.word.includes(guess)

  if (isUnique) {
    this.guessedLetters.push(guess)
  }

  if (isUnique && isBadGuess) {
    this.remainingGuesses--
  }
}

But this is below here how I wrote the if statement: 但这是我在下面编写if语句的方式:

Hangman.prototype.makeGuess = function(guess) {


  if (!this.guessedLetters.includes(guess)) {
    this.guessedLetters.push(guess)
  }

  if (!this.guessedLetters.includes(guess) && !this.word.includes(guess)) {
    this.remainingGuesses--
  }
}

The remaining guesses are not calculating correctly, if I do the if statements the second way. 如果我以第二种方式执行if语句,则剩余的猜测将无法正确计算。 Can you please tell me what is the difference? 你能告诉我有什么区别吗?

In your last example you change the guessedLetters array by using push() method. 在最后一个示例中,您使用push()方法更改了guessedLetters数组。 This may lead to a different result of the includes() method of that same array. 这可能导致同一数组的includes()方法产生不同的结果。

Try to undestate it yourself if you translate it in 'normal' English: 如果您将其翻译为“普通”英语,请尝试自行取消贬义:

  1. If guess is not in guesses list, add it to guesses list. 如果猜测不在猜测列表中,请将其添加到猜测列表。
  2. If guess is not in guesses list and not in word list, decrease remaining guesses 如果猜测不在猜测列表中且不在单词列表中,请减少剩余的猜测

This says. 这样说 If guess is not in guess, you will add it and the second condition will automatically be false, because you just added guess to the guesses list and so it is not NOT on the guesses list. 如果猜测不在猜测中,则将其添加,第二个条件将自动为假,因为您只是将猜测添加到了猜测列表中,因此它不在不在猜测列表中。

You can do it like this instead 你可以这样做

if (!this.guessedLetters.includes(guess)) {
    this.guessedLetters.push(guess)

    if (!this.word.includes(guess)) {
       this.remainingGuesses--
    }
}

The problem is here: 问题在这里:

if (!this.guessedLetters.includes(guess)) {
    this.guessedLetters.push(guess)
}

If this.guessedLetter didn't include guess , it's added to the list (with push ). 如果this.guessedLetter不包含guess ,则会将其添加到列表中(使用push )。

if (!this.guessedLetters.includes(guess)

You just modified this.guessedLetters . 您刚刚修改了this.guessedLetters So now, when you check again, guess is always there, so this condition is never true. 因此,现在,当您再次检查时, guess始终存在,因此这种情况永远不会成立。

In your example, try to analyze the flow of a case where the guessedLetters doesn't contain the guess : 在您的示例中,尝试分析guessedLetters 不包含 guess的情况下的guessedLetters

// if the guessedLetters doesn't contain guess ...
if (!this.guessedLetters.includes(guess)) {
    // ... then you add it
    this.guessedLetters.push(guess)
}

// and it already contains guess so the first clause of the if
// is always false
if (!this.guessedLetters.includes(guess) && !this.word.includes(guess)) {
    this.remainingGuesses--
}

In the original, they first compute both flags isUnique and isBadGuess and only then they modify guessedLetters and remainingGuesses . 在原始模型中,他们首先计算两个标志isUniqueisBadGuess ,然后才修改guessedLettersremainingGuesses guessedLetters The if clause checks the unmodified guessedLetters collection then. if子句然后检查未修改的guessedLetters集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM