-
Notifications
You must be signed in to change notification settings - Fork 0
/
word.js
72 lines (58 loc) · 2.71 KB
/
word.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// The Word constructor depends on the Letter constructor
var Letter = require("./Letter");
// The Word constructor is used to create an object representing
// the current word the user is attempting to guess
function Word (word) {
// word.split("") separates each character in the word and return an array of the letters of the underlying word
// .map returns an array containing the results of calling the Letter constructor on each letter
this.letters = word.split("").map(function (character) {
return new Letter(character);
})
};
// Setting the method for all Word objects on the protoype
// ==================================================================================
// Word.prototype.toString() returns a string representing the word.
// Because we name a function `toString`, JavaScript automatically call `toString` on each letter,
// then joins them together even if we don't call toString
Word.prototype.toString = function () {
// .letters call the function on each letter object that displays the character or an underscore
// .join(" ") concatenates those letters together.
return this.letters.join(" ");
};
// Word.prototype.hasThisLetter takes a character as an argument and
// calls the Letter.prototype.didExistinCurrentWord function on the letter object
// to see whether that guessed letter exists in the current word
Word.prototype.hasThisLetter = function (userGuessCharacter) {
var hasThisLetter = false;
this.letters.forEach(function (letterInTheWord) {
if (letterInTheWord.didExistinCurrentWord(userGuessCharacter) === true) {
hasThisLetter = true;
}
});
// Return whether that letter exists in the current word
return hasThisLetter;
};
// Word.prototype.guessedCorrectly returns `true` if all letters in the word have been guessed correctly.
Word.prototype.guessedCorrectly = function () {
return this.letters.every(function (letter) {
// If the value of letter.active on every letters are true,
// Word.prototype.guessedCorrectly will return `true`.
return letter.active;
});
};
// Get the underlying characters in order to show the user the correct word
Word.prototype.getAnswer = function () {
return this.letters.map(function (letter) {
// Call the underlying characters and join them together
return letter.showCharacter();
}).join("");
};
//Export the Word constructor so that we can use/reference it in game.js
module.exports = Word;
/* Test Word.js on its own
// ==================================================================================
// Drop the first two arguments in Node Js
process.argv.splice(0, 2);
var bible = new Word("Bible");
console.log(bible);
*/