Question
Modify the program of Project 8A to count the number of guesses the player makes. If the number is 10 or fewer, display Either you
Modify the program of Project 8A to count the number of guesses the player makes. If the number is 10 or fewer, display Either you know the secret or you got lucky! If the player guesses the number in 10 tries, display Ahah! You know the secret! If the player makes more than 10 guesses, display You should be able to do better!
Why should it take no more than 10 guesses? Well, with each good guess, the player should be able to eliminate half of the numbers. Do you know why any number between 1 to 1000 can be guessed in 10 or fewer tries? (You don't need to answer this question, but hopefully you understand why).
(Below is the HTML java code from the original code, this question askes to modify this code)
var max = 1000;
var guess = Math.floor(Math.random() * max) * 1;
label = document.getElementById("output");
function tryGuess() {
usrAnswer = Number(document.getElementById("guess").value);
if (usrAnswer < 0 || usrAnswer > 1000) {
alert("Please insert a number between 1 and 1000");
return;
} else {
if (guess != usrAnswer) {
if (guess < usrAnswer) {
label.innerHTML = "
Guessed too low:
";
} else if (guess > usrAnswer) {
label.innerHTML = "
Guessed too high:
";
}
} else {
label.innerHTML = "
Congratulations! You guessed the number! ";
label.innerHTML += "
You can play again.";
guess = Math.floor(Math.random() * max) * 1;
}
document.getElementById("guess").value = "";
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started