Get Programming with JavaScript - Listing 12.05
Listing 12.05 - Higher or Lower
JS Bin
var getGuesser = function () {
var secret = Math.floor(Math.random() * 10 + 1);
return function (userNumber) {
if (userNumber === secret) {
return "Well done!";
} else {
if (userNumber > secret) {
return "Too high!";
} else {
return "Too low!";
}
}
};
};
var guess = getGuesser();
Further Adventures
Listing 12.05 - Higher or Lower - Tasks 3 &
- Update the second condition to (userNumber < secret)
- Update the messages to match the new condition.
var getGuesser = function () {
var secret = Math.floor(Math.random() * 10 + 1);
return function (userNumber) {
if (userNumber === secret) {
return "Well done!";
} else {
if (userNumber < secret) {
return "Too low!";
} else {
return "Too high!";
}
}
};
};
var guess = getGuesser();
Listing 12.05 - Higher or Lower - Tasks 5 & 6
- Swap the first and second conditions so the second condition uses the strict equality operator.
- Update the messages to match the new condition.
var getGuesser = function () {
var secret = Math.floor(Math.random() * 10 + 1);
return function (userNumber) {
if (userNumber < secret) {
return "Too low!";
} else {
if (userNumber === secret) {
return "Well done!";
} else {
return "Too high!";
}
}
};
};
var guess = getGuesser();