console.log("Start file 1 =========================================");
function Letter (letter) {
this.letter = letter;
this.guess = false;
this.answer = function () {
if (!this.guess) {
return "_";
}
else if (this.guess === true) {
return this.letter;
}
}
this.letterTest = function (guessed) {
if (guessed === this.letter) {
this.guess = true;
// this.letter = guessed;
} else {
this.letter = "_";
}
}
};
module.exports = Letter;
console.log("End file 1 =========================================");
console.log("Start file 2 =========================================");
var Letter = require("./letter.js");
function Word (word) {
this.splitWord = [];
for (var i = 0; i < word.length; i++) {
var eachLetter = new Letter (word[i]);
this.splitWord.push(eachLetter);
}
this.stringRep = function () {
var testedWord = [];
for (var i = 0; i < this.splitWord.length; i++) {
testedWord.push(eachLetter.answer());
}
testedWord = testedWord.join(" ");
// console.log(testedWord);
return testedWord;
};
this.eachGuess = function (input) {
for (var i = 0; i < this.splitWord.length; i++) {
this.splitWord[i].letterTest(input);
}
}
}
module.exports = Word;
console.log("End file 2 =========================================");
console.log("Start file 3 =========================================");
var Word = require("./word.js");
var inquirer = require('inquirer');
var remainingGuesses = 10;
var mainGame;
var currentWord;
var liveWord = [];
completeWord = null;
let countryPicker;
let testedWord;
var europe = ["Albania", "Austria", "Belgium", "Bulgaria", "Croatia", "Cyprus", "Denmark", "England", "France", "Greece", "Germany",
"Hungary", "Iceland", "Italy", "Lithuania", "Monaco", "Norway", "Poland", "Portugal", "Romania", "Serbia", "Slovakia", "Spain", "Sweden",
"Switzerland", "Ukraine"];
// Function that picks a random country
function pickCountry () {
countryPicker = europe[Math.floor((Math.random() * europe.length) + 1)];
var mainGame2 = new Word (countryPicker);
mainGame = mainGame2;
// Display word
currentWord = mainGame.stringRep();
currentWord = JSON.stringify(currentWord);
console.log("Word: " + currentWord);
};
pickCountry();
// Delete this after
console.log("1: " + countryPicker);
console.log("2: " + currentWord);
inquirer.prompt([
{
type: "input",
name: "game",
message: "Guess a letter!"
}
]).then(function(inquirerResponse) {
liveWord = mainGame.splitWord;
mainGame.eachGuess(inquirerResponse.game);
for (let i = 0; i < liveWord.length; i++) {
console.log(mainGame.splitWord[i].letter);
}
});
I built a hangman game using constructor functions. I have set it up so when a random word is chosen, each letter will be displayed as an underscore.
I used the inquirer package to ask for a letter. When the first letter is guessed correctly it successfully replaces the the underscore with the letter in the liveWord array. The problem is, it only works for one letter. I need to make it work until the full word is guessed. FYI File 1 and 2 are only there for reference, my problem is only in file 3. No need to look at the first 2. Any tips?