lundi 30 janvier 2017

How do I get event.key to display each letter in my html?

Thank you for taking the time to help!

I am working on a hangman game and am running into a small issue. I am able to see the userInput if it is any other letter than what's below in my if else statements. The problem is that I want it to display that event, and then display any other event that wasn't already keyed to displayed alongside. For example: if userInput is === "k" then userInput is === "b", I would like it to keep the display of "k" in my html and then alongside it "b".

Also if there is a better way to write my if else statement with a loop or using forEach that would be helpful. I am new to the concepts. Thanks again.

   document.onkeyup = function(event) {
            var userInput = event.key;

if (currentWord === "taco"){ if (userInput === "t" ) { document.getElementById("1st-letter").innerHTML = userInput; } else if (userInput === "a") { document.getElementById("2nd-letter").innerHTML = userInput; } else if (userInput === "c") { document.getElementById("3rd-letter").innerHTML = userInput; } else if (userInput === "o") { document.getElementById("4th-letter").innerHTML = userInput; } else { document.getElementById("incorrect-letters").innerHTML = userInput; } } else { alert("Code is working"); } };

Aucun commentaire:

Enregistrer un commentaire