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

This is the last time I\'m going to bother posting this question because nobody

ID: 665022 • Letter: T

Question

This is the last time I'm going to bother posting this question because nobody can seem to understand that it needs to be written in HTML & JavaScript using JSFiddle and that it is the COMPUTER guessing the numbers inputted by the user, not the other way around like in most cases. Anyways if you could answer it I would be super greatful and screenshots would be awesome too as I'm a visual learner.

Using 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 the computer 6 Tries.

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

Explanation / Answer

Answer

HTML
<fieldset>
<legend>Inputs</legend>
<label for="guess">Pick a number between 1 and 1000</label>
<input type="text" id="guess" />
<input type="button" value="submit" /><br />

<input type="button" value="New Game" /><br />

</fieldset>
<fieldset id="guesses" class="guesses">
<legend> Output </legend>
<textarea id="output" name="output" rows="5"></textarea>
</fieldset>

JAVASCRIPT

function yourGuess() {
    // You can store references to the value and the
    // guesses textarea in local function variables.
    var guess = document.getElementById("guess").value;
    var guesses = document.getElementById("output");

    // First, if the guess is the same, just show the answer.
    // Append onto the textarea the result.
    if (guess == numToGuess) {
        guesses.value = guesses.value + " " + "You have guessed correctly! ("+guess+")";
    } else if (guess > numToGuess) {
        guesses.value = guesses.value + " " + "You guessing too high!("+guess+")";
    } else {
        guesses.value = guesses.value + " " + "You guessing too low!("+guess+")";
    }
}

// Randomly generate a number
function generateNumberToGuess(confirmIt) {
    var guesses = document.getElementById("output");

  
    // argument was passed.
    if (confirmIt && !confirm('Restart game with new number?')) {
        return;
    }

    guesses.value = '';
    numToGuess = Math.floor(Math.random()*500);
    guesses.value = "New number generated. ";


    document.getElementById('numberToGuess').value = '';
    document.getElementById('cheatShow').style.display = 'none';
}

window.onload = function(){
    generateNumberToGuess();
}

CSS
.guesses {
display: display;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote