Created
February 3, 2011 15:11
-
-
Save kentbrew/809588 to your computer and use it in GitHub Desktop.
In response to Adam Dachis' Lifehacker article, at http://lifehac.kr/ekdUVW
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<title>Guess My Number</title> | |
</head> | |
<body> | |
<h3 id="t"></h3> | |
<ul id="o"></ul> | |
<input id="g" /> | |
<button id="b">Guess</button> | |
<script> | |
var max = 100; | |
var n = Math.floor(Math.random() * max) + 1; | |
var d = document; | |
var o = d.getElementById('o'); | |
var g = d.getElementById('g'); | |
var b = d.getElementById('b'); | |
var t = d.getElementById('t'); | |
t.innerHTML = "I'm thinking of a number between 1 and " + max + "...."; | |
b.onclick = function() { | |
var v = parseInt(g.value); | |
if (v) { | |
if (v === n) { | |
respond(v + " is correct! Winner! We have a winner!"); | |
g.parentNode.removeChild(g); | |
b.innerHTML = "Go again?"; | |
b.onclick = function() { | |
d.location.reload(); | |
} | |
} else { | |
if (v < n) { | |
respond(v + " is too low."); | |
} else { | |
respond(v + " is too high."); | |
} | |
} | |
} | |
}; | |
var respond = function(r) { | |
var li = d.createElement('LI'); | |
li.innerHTML = r; | |
o.appendChild(li); | |
g.value = ''; | |
}; | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment