Get Programming with JavaScript - Listings 13.13 and 13.14
Listings 13.13 and 13.14 - Importing Kallie's namespace
JS Bin
HTML:
<!-- number generator -->
<script src="http://output.jsbin.com/qezoce.js"></script>
<!-- spacer -->
<script src="http://output.jsbin.com/juneqo.js"></script>
<!-- Kallie’s code -->
<script src="http://output.jsbin.com/moheka.js"></script>
JS:
var getGuesser = function (lowest, highest) {
var secret = between(lowest, highest);
return function (userNumber) {
var msg;
if (userNumber === secret) {
msg = kalliesCode.dasher("Well done!");
return spacer.wrap(msg, msg.length + 4, "=");
} else if (userNumber > secret) {
msg = kalliesCode.dasher("Too high!");
return spacer.wrap(msg, msg.length + 4, "+");
} else {
msg = kalliesCode.dasher("Too low!");
return spacer.wrap(msg, msg.length + 4, "-");
}
};
};
var guess = getGuesser(5, 10);
Further Adventures
Listings 13.13 and 13.14 - Importing Kallie's namespace - Task 2
- Change the program to use spreader rather than dasher, using the same character throughout each message.
var getGuesser = function (lowest, highest) {
var secret = between(lowest, highest);
return function (userNumber) {
var msg;
if (userNumber === secret) {
msg = kalliesCode.spreader("Well done!", "=");
return spacer.wrap(msg, msg.length + 4, "=");
} else if (userNumber > secret) {
msg = kalliesCode.spreader("Too high!", "+");
return spacer.wrap(msg, msg.length + 4, "+");
} else {
msg = kalliesCode.spreader("Too low!", "-");
return spacer.wrap(msg, msg.length + 4, "-");
}
};
};
var guess = getGuesser(5, 10);
Listings 13.13 and 13.14 - Importing Kallie's namespace - Tasks 3 & 4
- Create your own private wrap function, within the scope of getGuesser. It should use spreader and wrap to create a formatted message.
- Update the function returned from getGuesser to use your wrap function.
var getGuesser = function (lowest, highest) {
var secret = between(lowest, highest);
var wrap = function (text, character) {
var msg = kalliesCode.spreader(text, character);
return spacer.wrap(msg, msg.length + 4, character);
};
return function (userNumber) {
if (userNumber === secret) {
return wrap("Well done!", "=");
} else if (userNumber > secret) {
return wrap("Too high!", "+");
} else {
return wrap("Too low!", "-");
}
};
};
var guess = getGuesser(5, 10);