Skip to content

Instantly share code, notes, and snippets.

@erottman
Created November 15, 2016 23:10
Show Gist options
  • Save erottman/26c9ecf45cb83ff2afa6bde4203876d5 to your computer and use it in GitHub Desktop.
Save erottman/26c9ecf45cb83ff2afa6bde4203876d5 to your computer and use it in GitHub Desktop.
var words= ["goat", "cheese", "milk"];
var pickWord = function() {
return words[Math.floor(Math.random() * words.length)];
};
var word = pickWord();
var setupAnswerArray = function(word) {
var myArray =[];
for(var i = 0; i < word.length; i++) {
myArray[i] = "_";
}
return myArray;
};
var answerArray = setupAnswerArray(word);
var remainingLetters = word.length;
var showPlayerProgress = function(answerArray) {
alert("The Secret Word is " + answerArray.join(" "));
};
var getGuess = function() {
return prompt("Pick a Letter, any Letter");
};
var updateGameState = function(guess, word, answerArray) {
for(var j = 0; j < word.length; j++) {
if(word[j] === guess) {
answerArray[j] = guess;
remainingLetters--;
}
}
}
var showAnswerAndCongratulatePlayer = function(answerArray) {
alert("Winner!!" + answerArray.join(" "))
}
while(remainingLetters > 0) {
showPlayerProgress(answerArray);
var guess = getGuess();
if(guess === null) {
break;
}else if(guess.length !== 1) {
alert("please enter a single letter");
}else {
var correctGuesses = updateGameState(guess, word, answerArray);
remainingLetters -= correctGuesses;
}
};
showAnswerAndCongratulatePlayer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment