Skip to content

Instantly share code, notes, and snippets.

@erottman
Created November 12, 2016 20:59
Show Gist options
  • Save erottman/9c74eb05916eab7d3819c5fbe2ce9efe to your computer and use it in GitHub Desktop.
Save erottman/9c74eb05916eab7d3819c5fbe2ce9efe to your computer and use it in GitHub Desktop.
//create an array of words
var words =["java","monkey","amazing","pankcake"];
//pick a randon words
var word= words[Math.floor(Math.random() * words.length)];
//set up the answer array
var answerArray=[];
for(var i=0; i< word.length; i++){
answerArray[i]="_";
}
var remainingLetters=word.length;
//The game loop
while(remainingLetters >= 0) {
//show the player their progress
alert(answerArray.join(" "));
//Get a guess from the player
var guess=prompt("guess a letter, or click cancel to stop playing");
//add guesses to an answerArray
var guessAll="";
guessAll+=guess;
//convert toLowerCase
var guessLower=guess.toLowerCase();
if(guessLower===null){
break;
}else if(guessAll.length >4) {
break;
} else if(guessLower.length !== 1){
alert("Please pick single character");
}else{
//update the game state with the guess
for(var j=0; j<word.length; j++){
if(word[j] ===guessLower){
answerArray[j]= guessLower;
remainingLetters--;
}
}
}
}
//the end of the game loops
alert(answerArray.join(" "));
alert("You are a Good Guesser!! the answer was " + word);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment