I’m stuck on the last part of my code in which I need to create a text-box that will display the users number of attempts. What I don’t understand is how exactly to get the text-box to display the attempts. I have included the code. Thank you
<HTML>
<HEAD>
</HEAD>
<BODY>
<FORM NAME="testform">
<BR>
<INPUT TYPE="button" NAME="button" VALUE="Click to Guess" onClick="testButton(this.form)">
<BR>
</FORM>
<SCRIPT type="text/javascript">
function testButton (form){
varguess = prompt("Guess a number betweent 1 and 20.","")
varattempts = 0
switch(true)
{
case varguess == 14:
alert("You guessed right!")
break;
case varguess>0 && varguess<=13:
alert("Try again!")
varattempts = varattempts + 1
break;
case varguess>14 && varguess<=20:
alert("Try again!")
varattempts = varattempts + 1
break;
default:
alert("Please pick a number between 1 and 20.")
}
}
</SCRIPT>
<INPUT TYPE="text" NAME="inputbox" VALUE="Attempts " + varattempts">
</BODY>
</HTML>
I didn’t completely rewrite your code, but is this the desired effect: jsFiddle
I added an ID to your input field to make grabbing it easier so the HTML is:
BTW tags arenormally coded in lowercase (e.g.
<form>instead of<FORM>)And the JavaScript is:
Note that you need to move your varattempts variable outside of your function otherwise you’re always initializing it to 1 whenever you call it.