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

Using your JSFiddle account you are going to create a guessing game, only it wil

ID: 664971 • Letter: U

Question

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). Please post all the HTML, CSS, and Javascript you used to create the game with screenshots or links if able. Remember it's the computer guessing the number and not the user; the user just inputs the number for the computer to get until it gets it right.

Explanation / Answer

HTML & Javascript :

<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>Number Guesser</title>
<link rel="stylesheet" type="text/css" href="numGuess.css" />
</head>

<body>
<SCRIPT LANGUAGE="JavaScript">
var guessme=Math.round(Math.random()*(999)+1);
var speech='Guess my number (from 1 to 1000)';
var count=0;
function process(mystery) {
count++;
var guess=document.forms.guessquiz.guess.value;
var speech='"'+guess+ '" does not make sense to me.';
document.forms.guessquiz.guess.value='';

if (guess==mystery)
{
document.forms.guessquiz.prompt.value='Got it '+mystery+' is correct!';
alert ('The guess number is '+mystery+ ' it will take ' + count+ ' Guesses! Press this button to reload the page for another game.');
speech='';
document.location=document.location;
}

if (mystery<guess)
{
speech='Guessed '+ guess + ' Too high';
}

if (mystery>guess)
{
speech='Guessed '+ guess +' Too low';
}

if (guess=='')
{
speech='You didn't guess anything!'
}

document.forms.guessquiz.prompt.value=speech; document.forms.guessquiz.guess.focus();

}

// end hide -->
</SCRIPT>

<FORM NAME="guessquiz">
<CENTER>
<INPUT TYPE="text" NAME="prompt" SIZE="31" MAXLENGTH="40" VALUE="Guess my number (from 1 to 1000)"><BR>
<INPUT TYPE="text" NAME="guess" SIZE="4" MAXLENGTH="4" VALUE="">
<INPUT TYPE="button" VALUE="Guess">
</CENTER>

</FORM>
</body>
</html>

CSS:

.login {
height:100px;
width:170px;
margin:auto;
border:1px #CCC solid;
padding:10px;
background-color:#E9E9E9 }

input {
   background: #E1FFE1;
   border:1px #CCC solid;
   padding:5px;
}