Get Programming with JavaScript - Listings 13.05 and 13.06

Listings 13.05 and 13.06 - Using the Number Generator in the guessing game

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);

Further Adventures

Listings 13.05 and 13.06 - Using the Number Generator in the guessing game - Task 3

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);