Skip to content

Commit fe60f0b

Browse files
author
Max Kelly
committed
The Class Syntax
1 parent d954dc8 commit fe60f0b

File tree

2 files changed

+89
-102
lines changed

2 files changed

+89
-102
lines changed

Diff for: hangman/hangman.js

+57-64
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,71 @@
11
// No guesses? -> ***
22
// Guessed "c", "b" and "t"? -> c*t
3+
class Hangman {
4+
constructor(word, remainingGuesses) {
5+
this.word = word.toLowerCase().split("");
6+
this.remainingGuesses = remainingGuesses;
7+
this.guessedLetters = ["job"];
8+
this.status = "Playing";
9+
}
310

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
11+
// The below is a status report. It will either be finished of failed depending on the guesses you make.
12+
calculateStatus() {
13+
const finished = this.word.every(letter => this.guessedLetters.includes(letter));
1014

11-
const Hangman = function (word, remainingGuesses) {
12-
const wordAlteration = (word.toLowerCase()).split('')
13-
this.word = word.toLowerCase().split('')
14-
this.remainingGuesses = remainingGuesses
15-
this.guessedLetters = ['job']
16-
this.status = 'Playing'
17-
}
15+
if (this.remainingGuesses === 0) {
16+
this.status = "Failed"; // .join - Joins the array together so it doesn't come out like c,a,t instead cat
17+
} else if (finished) {
18+
this.status = "Finished";
19+
} else {
20+
this.status = "Playing";
21+
}
22+
}
1823

19-
// The below is a status report. It will either be finished of failed depending on the guesses you make.
20-
Hangman.prototype.calculateStatus = function() {
24+
getStatusMessage() {
25+
if (this.status === "Playing") {
26+
return `Guesses left: ${this.remainingGuesses}`;
27+
} else if (this.status === "Failed") {
28+
return `Nice try! The word was: "${this.word.join('')}"`;
29+
} else {
30+
return "Great work! You guessed the word";
31+
}
32+
}
2133

22-
const finished = this.word.every((letter) => this.guessedLetters.includes(letter))
34+
getPuzzle() {
2335

24-
if (this.remainingGuesses === 0) {
25-
this.status = 'Failed' // .join - Joins the array together so it doesn't come out like c,a,t instead cat
26-
} else if (finished) {
27-
this.status = 'Finished'
28-
} else {
29-
this.status = 'Playing'
30-
}
31-
};
36+
let puzzle = "";
3237

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'
38+
this.word.forEach(letter => {
39+
if (this.guessedLetters.includes(letter) ||letter === " ") {
40+
puzzle += letter;
41+
} else {
42+
puzzle += "*";
43+
}
44+
});
45+
46+
return puzzle;
4047
}
41-
}
4248

43-
Hangman.prototype.getPuzzle = function () {
44-
let puzzle = ''
45-
46-
this.word.forEach((letter) => {
47-
if (this.guessedLetters.includes(letter) || letter === ' ') {
48-
puzzle += letter
49-
} else {
50-
puzzle += '*'
49+
makeGuess(guess) {
50+
const aGuess = guess.toLowerCase();
51+
const isUnique = !this.guessedLetters.includes(guess);
52+
const isBadGuess = !this.word.includes(aGuess);
53+
54+
// If this.guessedLetters is empty & bad guess:
55+
if (this.status !== "Playing") {
56+
return;
5157
}
52-
})
53-
return puzzle
54-
}
5558

56-
Hangman.prototype.makeGuess = function (guess){
57-
const aGuess = guess.toLowerCase()
58-
//console.log(aGuess)
59-
const isUnique = !this.guessedLetters.includes(guess)
60-
const isBadGuess = !this.word.includes(aGuess)
61-
// If this.guessedLetters is empty & bad guess:
62-
63-
if (this.status !== 'Playing') {
64-
return;
65-
}
66-
67-
if (isUnique) {
68-
this.guessedLetters.push(aGuess)
69-
// bad guess - lose 1 guess
70-
this.guesses = this.guesses -1
71-
// If empty & good guess:
72-
}
59+
if (isUnique) {
60+
this.guessedLetters.push(aGuess);
61+
// bad guess - lose 1 guess
62+
this.guesses = this.guesses - 1;
63+
// If empty & good guess:
64+
}
7365

74-
if (isUnique && isBadGuess) {
75-
this.remainingGuesses-- // This -- at the end performs the same way as this.remainingGuesses = this.remainingGuesses -1
66+
if (isUnique && isBadGuess) {
67+
this.remainingGuesses--; // This -- at the end performs the same way as this.remainingGuesses = this.remainingGuesses -1
68+
}
69+
this.calculateStatus();
7670
}
77-
this.calculateStatus()
78-
}
71+
}

Diff for: oop/person.js

+32-38
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,37 @@
1-
// Prototypal Inheritance -
2-
3-
// The below is a constructor function. This allows us to set up unique objects and still let them be flexible
4-
// You cannot use an arrow function for this as it does not bind this.
5-
const Person = function (firstName, lastName, age, likes = []) { // If likes isn't provided then create a default value
6-
this.firstName = firstName
7-
this.lastName = lastName
8-
this.age = age
9-
this.likes = likes
10-
}
11-
12-
//------ Prototypal Inheritance ------
13-
// Prototype adds this to all the instances
14-
Person.prototype.getBio = function () {
15-
let bio = `${this.firstName} is ${this.age}.`
16-
17-
// We are able to use an arrow function because the parent is able to access the this statement.
18-
// Arrow functions don't bind this.
19-
this.likes.forEach((like) => {
20-
bio += ` ${this.firstName} likes ${like}`
21-
})
22-
23-
return bio;
24-
}
25-
26-
Person.prototype.setName = function(fullName) {
27-
const names = fullName.split(' ') // This .split allows us to split a string and make an array. We're currently splitting on a space. This would create just Andrew
28-
this.firstName = names[0]
29-
this.lastName = [1]
1+
// Prototypal Inheritance
2+
3+
//---- Class Syntax -----
4+
// What makes the person class is the new way to create a constructor function. You define the constructor inside the class instead of inside the function.
5+
// We never need to manually call the constructor - it gets called automatically when we use the new operator with the class name picked out
6+
class Person {
7+
8+
constructor(firstName, lastName, age, likes = []) {
9+
this.firstName = firstName;
10+
this.lastName = lastName;
11+
this.age = age;
12+
this.likes = likes;
13+
}
14+
15+
getBio() {
16+
let bio = `${this.firstName} is ${this.age}.`;
17+
18+
this.likes.forEach(like => {
19+
bio += ` ${this.firstName} likes ${like}`;
20+
});
21+
22+
return bio;
23+
}
24+
25+
setName(fullName) {
26+
const names = fullName.split(" "); // This .split allows us to split a string and make an array. We're currently splitting on a space. This would create just Andrew
27+
this.firstName = names[0];
28+
this.lastName = [1];
29+
}
3030
}
3131
// ---------------------
3232
// The below creates a new person and logs it to the console.
3333
const me = new Person('Max', 'Kelly', 21, ['surfing', 'SnowBoarding'])
34-
35-
me.getBio = function () {
36-
return 'This is fake!'
37-
}
38-
39-
me.setName('Andrew Smith')
40-
console.log(me.getBio())
41-
34+
me.setName('Andrew Smith')
35+
console.log(me.getBio())
4236
const person2 = new Person('John', 'Boob', 29)
43-
console.log(person2.getBio())
37+
console.log(person2.getBio())

0 commit comments

Comments
 (0)