I’m having trouble with my JavaScript code. When I press the button, nothing happens at all. The program is supposed to count how many attempts the computer needs to guess the number I enter in the input field.
I’m working in Adobe Dreamweaver and can’t figure out what’s going wrong. Here’s my code:
<html>
<head>
<meta charset="utf-8">
<title>Number Guessing Game</title>
</head>
<body>
<p>Pick a number from 1-6:</p>
<input id="userInput" type="text" />
<button id="startGame">Begin Game</button>
<script>
document.getElementById('startGame').onclick = function() {
var targetNumber = document.getElementById('userInput').value;
var foundMatch = false;
var attemptCount = 0;
while(foundMatch == false) {
var computerGuess = Math.random();
computerGuess = computerGuess * 6;
computerGuess = Math.floor(computerGuess) + 1;
if(computerGuess == targetNumber) {
foundMatch = true;
alert("Computer found your number!");
alert("It took " + attemptCount + " attempts");
} else {
attemptCount += 1;
}
}
}
</script>
</body>
</html>
Any ideas what might be causing this issue? The button doesn’t seem to trigger anything when clicked.