I just started learning 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:
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:
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:
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 with the second way. Can you please tell me what is the difference?
Aucun commentaire:
Enregistrer un commentaire