Listings 13.05 and 13.06 - Using the Number Generator in the guessing game
JS Bin
HTML:
<!-- Number Generator -->
<script src="http://output.jsbin.com/qezoce.js"></script>
JS:
var getGuesser = function (lowest, highest) {
var secret = between(lowest, highest);
return function (userNumber) {
if (userNumber === secret) {
return "Well done!";
} else if (userNumber > secret) {
return "Too high!";
} else {
return "Too low!";
}
};
};
var guess = getGuesser(5, 10);
Listings 13.05 and 13.06 - Using the Number Generator in the guessing game - Task 3
- Update the game so that it keeps track of the number of guesses a player takes to find the secret number.
var getGuesser = function (lowest, highest) {
var secret = between(lowest, highest);
var numGuesses = 0;
return function (userNumber) {
numGuesses += 1;
if (userNumber === secret) {
return "Well done! You took " + numGuesses + " guesses.";
} else if (userNumber > secret) {
return "Too high!";
} else {
return "Too low!";
}
};
};
var guess = getGuesser(5, 10);