Skip to content

Commit d954dc8

Browse files
author
Max Kelly
committed
Hangman Challenge: Part IV
1 parent a1f3d36 commit d954dc8

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

hangman/app.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
1-
// 1. Setup new "status" property with initial value of "playing"
2-
// 2. Create method for recalculating status to "playing", "finished" or "Failed"
3-
// 3. Call that method after a guess is processed
4-
// 4. Use console.log to print the status
51

62
const puzzleEl = document.querySelector("#puzzle"); // Selects the id element in html file.
7-
const guessesEl = document.querySelector("#guesses"); // Selects the id element in html file.
83
const statusEl = document.querySelector('#status')
94
const game1 = new Hangman("Cat", 2);
105

116
puzzleEl.textContent = game1.getPuzzle();
12-
guessesEl.textContent = game1.remainingGuesses;
13-
statusEl.textContent = game1.status
7+
statusEl.textContent = game1.getStatusMessage()
148

159
window.addEventListener("keypress", function(e) {
1610
const guess = String.fromCharCode(e.charCode);
1711
game1.makeGuess(guess);
1812
puzzleEl.textContent = game1.getPuzzle();
19-
guessesEl.textContent = game1.remainingGuesses;
20-
statusEl.textContent = game1.status
13+
statusEl.textContent = game1.getStatusMessage()
2114
});
2215

2316

hangman/hangman.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,45 @@
11
// No guesses? -> ***
22
// Guessed "c", "b" and "t"? -> c*t
33

4+
// 1. Disable new guesses unless "playing"
5+
// 2. Setup a new method to get back a status message
6+
7+
// Playing -> Guesses left: 3
8+
// Failed -> Nice Try! The word was "Cat"
9+
// Finished -> Great work! You guessed the word
10+
411
const Hangman = function (word, remainingGuesses) {
512
const wordAlteration = (word.toLowerCase()).split('')
613
this.word = word.toLowerCase().split('')
714
this.remainingGuesses = remainingGuesses
815
this.guessedLetters = ['job']
9-
this.status = 'playing'
16+
this.status = 'Playing'
1017
}
1118

1219
// The below is a status report. It will either be finished of failed depending on the guesses you make.
1320
Hangman.prototype.calculateStatus = function() {
1421

15-
let finished = true
16-
this.word.forEach((letter) => {
17-
if (this.guessedLetters.includes(letter)) {
18-
finished = true
19-
} else {
20-
finished = false
21-
}
22-
})
22+
const finished = this.word.every((letter) => this.guessedLetters.includes(letter))
2323

2424
if (this.remainingGuesses === 0) {
25-
this.status = 'Failed'
25+
this.status = 'Failed' // .join - Joins the array together so it doesn't come out like c,a,t instead cat
2626
} else if (finished) {
2727
this.status = 'Finished'
2828
} else {
2929
this.status = 'Playing'
3030
}
3131
};
3232

33+
Hangman.prototype.getStatusMessage = function () {
34+
if (this.status === 'Playing') {
35+
return `Guesses left: ${this.remainingGuesses}`
36+
} else if (this.status === 'Failed') {
37+
return `Nice try! The word was: "${this.word.join('')}"`
38+
} else {
39+
return 'Great work! You guessed the word'
40+
}
41+
}
42+
3343
Hangman.prototype.getPuzzle = function () {
3444
let puzzle = ''
3545

@@ -49,6 +59,11 @@ Hangman.prototype.makeGuess = function (guess){
4959
const isUnique = !this.guessedLetters.includes(guess)
5060
const isBadGuess = !this.word.includes(aGuess)
5161
// If this.guessedLetters is empty & bad guess:
62+
63+
if (this.status !== 'Playing') {
64+
return;
65+
}
66+
5267
if (isUnique) {
5368
this.guessedLetters.push(aGuess)
5469
// bad guess - lose 1 guess

hangman/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
</head>
99
<body>
1010
<p id="puzzle"></p>
11-
<p id="guesses"></p>
1211
<p id="status"></p>
1312

1413

0 commit comments

Comments
 (0)