Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Probabilistic Databases

Authors: Dan Suciu, Dan Olteanu, Christopher Re, Christoph Koch

1st Edition

3031007514, 978-3031007514

More Books

Students also viewed these Databases questions

Question

What is DDL?

Answered: 1 week ago