JSFiddle - React, Tailwind, and code Playground
by chris richarde
HTML
<input type="textbox" id="number" value="2" />
<br/>
<input type="button" value="start guessing" onClick="guessNumber();" />
<br/>
<div id="output">
</div>
JavaScript
var myNum =
parseInt(document.getElementById("number").value);
function clearDisplay() {
d = "";
document.getElementById("output").innerHTML = "";
function guessNumber();
{
if(myNum < 1 || myNum >1000)
{
document.getElementById('output').innerHTML = "please enter number that is between 1 and 1000" + "<br/>";
}
else if (myNum >= 1 || myNum <=1000)
{
var compGuess = Math.floor(Math.random() * 1000 + 1);
while(compGuess != myNum )
{
if(compGuess < myNum)
{
document.getElementById('output').innerHTML = "guessed " + compGuess + "- to low!" + "<br/>"
var compGuess = Math.floor(Math.random() * 1000 + compGuess);
}
else if(compGuess > myNum)
{
document.getElementById('output').innerHTML = "guessed " + compGuess + "- too high!" + "<br/>";
var compGuess = Math.floor(Math.random() * compGuess + 1);
}
}
document.getElementById('output').innerHTML = "guessed " + compGuess + "- got it!" + "<br/>";
}