I have created this javascript which generates a random number ranging from 0-20. What I want to do is create a text field where the user has 4 attempts to guess the generated number. How would I do this?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>
<body>
<form><input name="code" id="code" type="text" value="" ></input>
<script type="text/javascript">
function makeid() {
return Math.floor(Math.random() * 20)
}
</script>
<input type="button" style="font-size:9pt" value="Generate Code" onclick="document.getElementById('code').value = makeid()">
</input>
</form>
</body>
</html>
You could use a global to track state. However, the security of this is weak, as the user could simply reload the page to circumvent the limit. What you really need to do then is track state on the server side (e.g. database) and then use e.g. an Ajax Call on the browser to do the checking 🙂
Edit
Apologies, I’ve only answered half your question.
And then change your click handler to