Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using your JavaScript and your JSFiddle account you are going to create a guessi

ID: 673780 • Letter: U

Question

Using your JavaScript and your JSFiddle account you are going to create a guessing game, only it will be the computer doing the guessing. Here is how it works - the computer will ask you for a number between 1 and 1000, it will check first to make sure your input is within the bounds.

Once you enter the number it will guess the number and do a comparison with the number you entered. It will output the results of the guess and continue to do this until it gets the correct answer. This is what the output of the program will look like (if I enter 329)

Guessed 500 - too high.

Guessed 250 - too low.

Guessed 375 - too high.

Guessed 313 - too low.

Guessed 344 - too high.

Guessed 329 - Got It!

It took me 6 Tries.

You can probably figure out how my algorithm works. You will want to create an algorithm that is efficient (lowest possible O).

Programming parameters:

1 - This will be coded in Javascript.

2 - You should create a dimensioned array of 1000 elements.

3 - You should fill in the elements from 1 to 1000 in order prior to implementing the search

Please include screenshots of the Javascript code and / or the temple used. Thank you!

Explanation / Answer

<p id="instructions">Guess a number between 0-1000. I will tell you if the number I am thinking of is greater than or less than your guess.</p>

<div>Hints:

    <div id="guessRecords"></div>

    <form id="guessForm">Your guess:

        <input type="number" min="0" max="1000" id="guess" />

        <input type="submit" value="Guess!" />

    </form>

</div>

JSFIDDLE code is:

var guessedCorrectly = false;

var randomNumber = Math.floor(Math.random() * 1000) + 1;

var yourGuess;

var allowedGuesses = 5;

var message = "Guess a number between 1 and 1000";

do {

yourGuess = parseInt(prompt(message));

allowedGuesses--;

if (isNaN(yourGuess) || yourGuess < 1 || yourGuess > 1000) // check for illegal entry

    message = "Illegal entry, # of guesses left " + allowedGuesses;

else if (yourGuess > randomNumber)

    message = "Your guess is too high, # of guesses left " + allowedGuesses;

else if (yourGuess < randomNumber)

    message = "Your guess is too low, # of guesses left " + allowedGuesses;

else {       // correctly guessed.

    guessedCorrectly = true;

    alert("You guessed correctly!");

    break;

}

} while (allowedGuesses > 0);

if (!guessedCorrectly) {

alert("sorry, you did not guess my number. My number was " + randomNumber);

}