Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,7 @@
|
|||
<p>Pick a number between 1 and 100.</p>
|
||||
<form id="guessNumber">
|
||||
<input type="text" name="guess">
|
||||
<input type="submit" value="Submit Guess">
|
||||
</form>
|
||||
<p id="output"></p>
|
||||
<script type="text/javascript">
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
var number = Math.ceil(Math.random() * 100);
|
||||
|
||||
function verify() {
|
||||
var guess = Number(this.elements.guess.value),
|
||||
output = document.getElementById('output');
|
||||
|
||||
if (isNaN(guess)) {
|
||||
output.innerHTML = 'Enter a number.';
|
||||
} else if (number === guess) {
|
||||
output.innerHTML = 'You guessed right!';
|
||||
} else if (guess > 100) {
|
||||
output.innerHTML = 'Your guess is out of the 1 to 100 range.';
|
||||
} else if (guess > number) {
|
||||
output.innerHTML = 'Your guess is too high.';
|
||||
} else if (guess < number) {
|
||||
output.innerHTML = 'Your guess is too low.';
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
document.getElementById('guessNumber').onsubmit = verify;
|
||||
|
|
@ -0,0 +1 @@
|
|||
</script>
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
#!/usr/bin/env js
|
||||
|
||||
function main() {
|
||||
guessTheNumber(1, 100);
|
||||
}
|
||||
|
||||
function guessTheNumber(low, high) {
|
||||
var num = randOnRange(low, high);
|
||||
var guessCount = 0;
|
||||
|
||||
function checkGuess(n) {
|
||||
if (n < low || n > high) {
|
||||
print('That number is not between ' + low + ' and ' + high + '!');
|
||||
return false;
|
||||
}
|
||||
|
||||
if (n == num) {
|
||||
print('You got it in ' + String(guessCount) + ' tries.');
|
||||
return true;
|
||||
}
|
||||
|
||||
if (n < num) {
|
||||
print('Too low.');
|
||||
} else {
|
||||
print('Too high.');
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
print('I have picked a number between ' + low + ' and ' + high + '. Try to guess it!');
|
||||
while (true) {
|
||||
guessCount++;
|
||||
putstr(" Your guess: ");
|
||||
var n = parseInt(readline());
|
||||
if (checkGuess(n)) break;
|
||||
}
|
||||
}
|
||||
|
||||
function randOnRange(low, high) {
|
||||
var r = Math.random();
|
||||
return Math.floor(r * (high - low + 1)) + low;
|
||||
}
|
||||
|
||||
main();
|
||||
Loading…
Add table
Add a link
Reference in a new issue