I need help here. In the addEventListener, the keypress is working but the result is always false(default when using switch or else when using if/else statement). I will put my code here so you will understand:
var input = document.getElementById("userAnswer");
input.addEventListener("keypress", function (enter){
if(enter.keyCode === 13){
return startGame();
}
}
function startGame(){
var userAnswer = document.getElementById("userAnswer");
//switch statement: always default
switch(userAnswer){
case "start":
codes... "Lets start the game!";
break;
default:
codes... "Looks like you misspelled start! Type start again!"
}
//if else statement: always else
if(userAnswer === "start"){
codes... "Lets start the game!";
} else {
codes... "Looks like you misspelled start! Type start again!"
}
}
Please help me guys! I'm creating a game app using visual studio, I can't use onclick on input because its js have an anonymous function so I add .addEventListener!
ADDED: In the result, it always says the else or default which is the "looks like you...", even when I type in the input "start" correctly. Are there any errors?