The Computer needs to Guess the number, not the user Using your JSFiddle account
ID: 3672754 • Letter: T
Question
The Computer needs to Guess the number, not the user
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 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
The point is for the computer to be guessing the number.
Explanation / Answer
save the following code with .html
----------------------
<SCRIPT LANGUAGE="JavaScript">
<!-- ;
var guessme=Math.round(Math.random()*(999)+1);
var speech='Guess my number (from 1 to 1000)';
function process(mystery) {
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='Congratulations! '+mystery+' is correct!';
alert ('Guessed: '+mystery+' got it');
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();
var inn = document.getElementById("steps").innerHTML;
inn=inn+speech+"</br>";
document.getElementById("steps").innerHTML=inn;
}
// end hide -->
</SCRIPT>
<FORM NAME="guessquiz">
<CENTER>
<TABLE ALIGN="CENTER" BGCOLOR="#555555" BORDER="3" CELLPADDING="5">
<TR>
<TD BGCOLOR="#889765">
<FONT COLOR="#ffffff" FACE="Arial"><B>GUESS MY NUMBER (1 - 1000)</B></FONT>
</TD>
</TR>
<TR>
<TD>
<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="3" MAXLENGTH="3" VALUE="">
<INPUT TYPE="button" VALUE="Guess">
<p id="steps"></p>
</CENTER>
</TD>
</TR>
</TABLE>
</CENTER>
</FORM>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.