-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_word.js
51 lines (46 loc) · 1.45 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
//Require Dependencies
var Letter = require('./_letter.js');
var randomWord = require('random-words');
//Word Constructor
var Word = function(){
this.currentWord = '';
this.guessedLetters = [];
this.getNewWord = function(){
this.currentWord = randomWord();
this.guessedLetters = []
//console.log(this.currentWord)
};
this.incorrectGuess = function(letter){
//Determines if the letter has already been guessed.
if (this.guessedLetters.indexOf(letter.toUpperCase()) === -1 && this.guessedLetters.indexOf(letter.toLowerCase()) === -1 ){
//Adds the guessed letter to the guessedLetters array.
this.guessedLetters.push(letter);
//Decreases the remainingGuesses for incorrect guess.
if (this.currentWord.indexOf(letter.toUpperCase()) === -1 && this.currentWord.indexOf(letter.toLowerCase()) === -1 ){
return true;
} else {
return false;
}
} else {
console.log('You already guessed the letter', letter);
}
}
this.display = function(){
var wordSoFar = '';
for (var i=0; i < this.currentWord.length; i++){
var letter = new Letter(this.currentWord[i]);
wordSoFar += letter.display(this.guessedLetters)
};
return wordSoFar;
};
};
//Export Word Module
module.exports = Word;
//Testing for Word Module
// var word = new Word();
// word.getNewWord()
// word.guessedLetters.push('h');
// word.guessedLetters.push('E')
// console.log(word.incorrectGuess('t'))
// console.log(word.guessedLetters);
// console.log(word.display());