Created
November 12, 2016 20:59
-
-
Save erottman/9c74eb05916eab7d3819c5fbe2ce9efe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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