Question
I am having trouble with creating a code in JScript that will let a user input a number between 1 and 100, have an array
I am having trouble with creating a code in JScript that will let a user input a number between 1 and 100, have an array and have the computer guess and display each guess and number of attempts when done. Not sure how to make a random integer bewteen the new numbers when a guess has been chosen.. Has to work with JSFiddle
HTML
Please input a number
JAVASCRIPT
var array = []; var min=0; var max=100;
var message = "Guess a number between 1 and 100";
var yourGuess = parseInt(document.getElementById("yourGuess").value);
function createArray() {
for (i = min, i <= max, i++){ var array[i]=i; } }
function guessGame(array, yourGuess) {
var count = 0; document.write(message); createArray();
while (array[comGuess] != yourGuess) {
var comGuess = Math.floor((max + min) / 2); count = count + 1; if (array[comGuess] < yourGuess) { message = "Computer guessed " + comGuess + " and is too LOW. "; document.write(message); min = comGuess;
} else if (array[comGuess] > yourGuess) { message = "Computer guessed " + comGuess + " and is too HIGH. "; document.write(message); max = comGuess; } else if (comGuess === yourGuess){ return comGuess; } } message= "I finally found your number! It is " + comGuess + " and it took me " + count + " guesses. "; document.write(message); }
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