Skip to content

Instantly share code, notes, and snippets.

@sealucky7
Last active December 26, 2018 15:16
Show Gist options
  • Save sealucky7/a26a92549e4145fc64a9b762b48a9f79 to your computer and use it in GitHub Desktop.
Save sealucky7/a26a92549e4145fc64a9b762b48a9f79 to your computer and use it in GitHub Desktop.
let words = [
"programm",
"monkeys",
"lucky",
"javascript"
];
let word = words[Math.floor(Math.random() * words.length)];
let answerArray = [];
for(let i = 0; i < word.length; i++) {
answerArray[i] = "_";
}
let remainingLetters = word.length;
while (remainingLetters > 0) {
alert(answerArray.join(" "));
let guess = prompt("Guess the letter or click Cancel to exit the game.");
if (guess === null) {
break;
} else if (guess.length !== 1) {
alert("Please enter only one letter.")
} else {
for (let j = 0; j < word.length; j++) {
if (word[j] === guess) {
answerArray[j] = guess;
remainingLetters--;
}
}
}
}
alert(answerArray.join(" "));
alert("Greate! It was a word " + word);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment