Skip to content

Commit a1f3d36

Browse files
author
Max Kelly
committed
Hangman Challenge: Part III
1 parent 8ab6d46 commit a1f3d36

File tree

4 files changed

+59
-19
lines changed

4 files changed

+59
-19
lines changed

hangman/app.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,39 @@
1-
// Primitive value: string, number, boolean, null, undefined
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
5+
6+
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.
8+
const statusEl = document.querySelector('#status')
9+
const game1 = new Hangman("Cat", 2);
10+
11+
puzzleEl.textContent = game1.getPuzzle();
12+
guessesEl.textContent = game1.remainingGuesses;
13+
statusEl.textContent = game1.status
14+
15+
window.addEventListener("keypress", function(e) {
16+
const guess = String.fromCharCode(e.charCode);
17+
game1.makeGuess(guess);
18+
puzzleEl.textContent = game1.getPuzzle();
19+
guessesEl.textContent = game1.remainingGuesses;
20+
statusEl.textContent = game1.status
21+
});
22+
23+
24+
25+
//----- Primitive value: string, number, boolean, null, undefined -----
226

327
// Object: myObject --> product --> Object.prototype --> null // This is how the below code will run it will go through product if nothing there it then goes through object.prototype if nothing there it returns undefined
428
// Array: myArray --> Array.prototype --> Object.prototype --> null
529
// Function: myFunc --> Function.prototype --> Object.prototype --> null
630
// String: myString --> String.prototype --> Object.prototype --> null
731
// Number: myNumber --> Number.prototype --> Object.prototype --> null
832
// Boolean: myBoolean --> Boolean.prototype --> Object.prototype --> null
33+
// const getString = 'Computer'
34+
// console.log(getString.split(''))
35+
36+
// const otherString = new String('Phone')
37+
// console.log(otherString)
938

10-
const getString = 'Computer'
11-
console.log(getString.split(''))
1239

13-
const otherString = new String('Phone')
14-
console.log(otherString)

hangman/hangman.js

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,30 @@ const Hangman = function (word, remainingGuesses) {
66
this.word = word.toLowerCase().split('')
77
this.remainingGuesses = remainingGuesses
88
this.guessedLetters = ['job']
9+
this.status = 'playing'
910
}
1011

12+
// The below is a status report. It will either be finished of failed depending on the guesses you make.
13+
Hangman.prototype.calculateStatus = function() {
14+
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+
})
23+
24+
if (this.remainingGuesses === 0) {
25+
this.status = 'Failed'
26+
} else if (finished) {
27+
this.status = 'Finished'
28+
} else {
29+
this.status = 'Playing'
30+
}
31+
};
32+
1133
Hangman.prototype.getPuzzle = function () {
1234
let puzzle = ''
1335

@@ -18,7 +40,7 @@ Hangman.prototype.getPuzzle = function () {
1840
puzzle += '*'
1941
}
2042
})
21-
return puzzle
43+
return puzzle
2244
}
2345

2446
Hangman.prototype.makeGuess = function (guess){
@@ -37,15 +59,5 @@ Hangman.prototype.makeGuess = function (guess){
3759
if (isUnique && isBadGuess) {
3860
this.remainingGuesses-- // This -- at the end performs the same way as this.remainingGuesses = this.remainingGuesses -1
3961
}
62+
this.calculateStatus()
4063
}
41-
42-
const game1 = new Hangman('Cat', 2)
43-
console.log(game1.getPuzzle())
44-
console.log(game1.remainingGuesses)
45-
46-
window.addEventListener('keypress', function (e) {
47-
const guess = String.fromCharCode(e.charCode)
48-
game1.makeGuess(guess)
49-
console.log(game1.getPuzzle());
50-
console.log(game1.remainingGuesses);
51-
})

hangman/index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
<meta name="viewport" content="width=device-width, initial-scale=1">
88
</head>
99
<body>
10-
10+
<p id="puzzle"></p>
11+
<p id="guesses"></p>
12+
<p id="status"></p>
1113

1214

1315
<script src="hangman.js"></script>
16+
<script src="app.js"></script>
1417

1518
</body>
1619
</html>

notes-app/notes-function.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ const renderNotes = (notes, filters) => {
106106
notes = sortNotes(notes, filters.sortBy)
107107
const filteredNotes = notes.filter( (note) => note.title.toLowerCase().includes(filters.searchText.toLowerCase()));
108108

109-
document.querySelector("#notes").innerHTML = ""; // This code allows us to whip the text that isn't relevant. This re renders the list
109+
document.querySelector("#notes").innerHTML = ""; // This code allows us to wipe the text that isn't relevant. This re renders the list
110110

111111
filteredNotes.forEach((note) => {
112112
const noteEl = generateNoteDOM(note); // This pulls code from notes-function.js

0 commit comments

Comments
 (0)