let totalScore = 0;
var myArray = [1, 2, 3, 4, 5];
let randomNum;
function NumGuess(userInput) {
var userGuess = userInput;
var i;
for (i = 0; i < 1; i++) {
var randomNum = myArray[Math.floor(Math.random() * myArray.length)];
}
if (userGuess == randomNum) {
alert("CONGRATULATIONS!!! YOU GUESSED IT RIGHT");
totalScore++;
} else {
alert("SORRY, The correct number was " + randomNum);
totalScore = totalScore - 1;
}
document.getElementById("GameScore").innerHTML = "Score: " + totalScore;
}
<!doctype HTML>
<html>
<body>
<p id="GameScore">Score: 0</p>
<button onclick="NumGuess(document.getElementById('userInput').value)">Start Game!</button>
<input id="userInput" type="text">
</body>
</html>
Trying to pass the user input as a parameter in the function instead of defining it within the function. However, faced with a challenge since user input is coming from a textbox triggered by a button click.