[英]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: 如果您将其翻译为“普通”英语,请尝试自行取消贬义:
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
. 在原始模型中,他们首先计算两个标志
isUnique
和isBadGuess
,然后才修改guessedLetters
和remainingGuesses
guessedLetters
。 The if
clause checks the unmodified guessedLetters
collection then. if
子句然后检查未修改的guessedLetters
集合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.