Skip to content

Instantly share code, notes, and snippets.

@kentbrew
Created February 3, 2011 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kentbrew/809588 to your computer and use it in GitHub Desktop.
Save kentbrew/809588 to your computer and use it in GitHub Desktop.
In response to Adam Dachis' Lifehacker article, at http://lifehac.kr/ekdUVW
<!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