I'm working on coding a hangman app and I have a question. How can I compare the user input "userGuess" to the array "array" that consists of letters split from the randomly selected word "word"?
If the "userGuess" matches any of the values in the "array", I want to log to the console: userGuess + "is correct".
$(document).ready(function() { console.log("I'm all set up and ready to go!");
var randomWords = [
"dog",
"cat",
"america",
"bootcamp",
"javascript",
"philadelphia"
]
var word = randomWords[Math.floor(Math.random() * randomWords.length)];
console.log(word);
var amount = word.length;
console.log(amount);
$("#display-word").on("click", function(event) {
$("#word").html("New Word is: " + amount + " letters long.")
})
//event listener
document.onkeyup = function(event) {
var userGuess = event.key;
console.log(userGuess);
$("#guesses").append(userGuess + "-");
var str = word;
var array = str.split("");
console.log(array);
for (var i = 0; i < array.length; i++) {
if (userGuess === array[i]) {
console.log(userGuess + " is correct");
}
}
}//end of keyup event
}); //end of document ready function