let score = 0;
var myArray = [1, 2, 3, 4, 5];
let randomNumber;
function GuessNumber() {
var userGuess = document.getElementById("userInput").value;
var i;
for (i = 0; i < 1; i++) {
var randomNumber = myArray[Math.floor(Math.random() * myArray.length)];
}
var userGuess = document.getElementById("userInput").value;
if (userGuess == randomNumber) {
alert("CONGRATULATIONS!!! YOU GUESSED IT RIGHT");
score++;
} else {
alert("SORRY, The correct number was " + randomNumber);
score = score - 1;
}
document.getElementById("GameScore").innerHTML = "Score: " + score;
}
<!doctype HTML>
<html>
<body>
<p id="GameScore">Score: 0</p>
<button onclick="GuessNumber()">Start Game!</button>
<input id="userInput" type="text">
</body>
</html>
So the whole point is that instead of writing
var userGuess = document.getElementById("userInput").value
I want the user's input to be a parameter for the function.
The problem however is that i'm using a button to start the function so i'm not sure what to do since the users input is with a textbox.