Question: Assignment Using your JSFiddle account you are going to create a guessing game, only it will be the computer doing the guessing. Here is how
Assignment
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. NOTE: THE USER IS NOT THE ONE GUESSING THE USER ENTERS A NUMBER AND THE COMPUTER OUTPUTS ITS GUESSES
This is what the output of the program will look like (if I enter 329). Once again the user inputs the number and the computer searches for it in the Array.
| User Input | Input: 329 |
| Computer Output | 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. |
What i did wrong:
The One did was were the user guesses which was wrong please help this is what I did.
The right way:
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. NOTE: THE USER IS NOT THE ONE GUESSING THE USER ENTERS A NUMBER AND THE COMPUTER OUTPUTS ITS GUESSES
JSFiddle
HTML Code:
Javascript + no-library (pure JS):
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(); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
