I'm trying to build a basic hangman game. I'm trying to get 'runcode' function to run once using closures. However I'm not sure why my closure is not working. I'm creating an object then using the map method to replace the letters with underscores.
const hangMan = () => {
const word = "honduras";
const arrayWord = word.split("");
let dashedWord = [];
const hashWord = () => {
dashedWord = arrayWord.map((el, index) => {
dashedWord.push(el);
return (el = "_");
});
// console.log(`dashedword function = ${dashedWord}`);
// return dashedWord;
};
// console.log(prevWord);
// console.log(hashedWord);
// console.log((arrayWord[0] = "t"));
// console.log(arrayWord.indexOf("t"));
// console.log(typeof arrayWord);
// console.log(arrayWord);
function findIndex(word, index) {
return word.indexOf(index);
}
function updateFoundLetter(guess) {
let executed = false;
console.log("fired updateFoundLetter");
const runcode = () => {
console.log(`UpdateFoundLetter wordHashed = ${executed}`);
if (!executed) {
hashWord();
executed = true;
console.log("ran true");
} else {
let index = findIndex(word, guess);
dashedWord[index] = guess;
}
//console.log("hashedword index " + dashedWord[index]);
// console.log(index);
};
return runcode;
}
//console.log(`before calling updateFoundLetter ${wordHashed}`);
let myFunc = updateFoundLetter("h", word);
myFunc();
console.log(dashedWord);
//console.log(`after calling updateFoundLetter ${wordHashed}`);
//console.log(findIndex(word, "o"));
};
hangMan();