When we return a function from a function & it is complete with its local variables etc, we call it a closure
result = 54
result = 40;
result : 122
Building a Closure I
The Dev Girls at the Cold Closures Cove sometimes need to provide warnings to travelers about various obstacles that sometimes float into the Cove. They need you to prepare a very efficient warning maker that will allow them to create only the warnings they need, and only when they need it. Closures to the rescue!
They’ve started a function called
They’ve started a function called
warningMaker with a parameter called obstacle. Within this function, return an anonymous function that displays a specific alert message based on the specific obstacleencountered. The format of the message should be as follows:Beware! There have been sightings in the Cove today!
Note: You do not need to call the warningMaker functionfunction warningMaker(obstacle) {return function(){alert("Beware! There have been "+obstacle+" sightings in the Cove today!");};}
function warningMaker(obstacle) {return function() {alert("Beware! There have been " + obstacle + " sightings in the Cove today!");};}var icebergAlert = warningMaker("iceberg");icebergAlert();
Building a Closure II
The Dev Girls admire your closure mastery. To test your might, they’ve asked you to modify thewarningMakerfunction in order to announce:
- The number of obstacles. This must be the first parameter.
- The specific location of the obstacle found. This must be the secondparameter.
In other words, they want to pass a specificnumberand a specificlocationto the function, and have those values become part of thealertmessage for theobstaclebeing passed into thewarningMakerfunction.Update thewarningMakerfunction so that it follows the format below. Use the parameters to take the names of the bracketed placeholders:Beware! There have beensightings in the Cove today! have been spotted at the ! Note: Pay close attention to whitespace! You do not need to call thewarningMakerfunction.function warningMaker(obstacle) {
return function(number, location) {
alert("Beware! There have been " + obstacle +
" sightings in the Cove today!\n" +
number + " have been spotted at the " +
location + "!");
};
}
var killerPenguinAlert = warningMaker("killer penguin");
var polarBearAlert = warningMaker("polar bear");
var icebergAlert = warningMaker("iceberg");
var flashBlizzardAlert = warningMaker("flash blizzard");
var snowYetiAlert = warningMaker("snow yeti");
killerPenguinAlert(6,"Ice Caves");
snowYetiAlert(1,"Blizzard Beach");
function warningMaker(obstacle) {
var count =0;
return function(number, location) {
count++;
alert("Beware! There have been " + obstacle +
" sightings in the Cove today!\n" +
number + " have been spotted at the " +
location + "!\n" +"This is alert #"+count+" today for "+obstacle+" danger.");
};
}
function warningMaker(obstacle) {var count = 0;var zones = [];return function(number, location) {count++;var list = "";zones.push(location);for (var i = 0; i < zones.length; i++) {list = list + zones[i]+"\n";}alert("Beware! There have been " + obstacle +" sightings in the Cove today!\n" +number + " have been spotted at the " +location + "!\n" +"This is alert #" + count +" today for " + obstacle + " danger.\n" +list);};}
Just Keep Track of It All!
Well, it’s nice for new travelers to know where the danger zones are, but what if some of them are thrill-seekers? They might actually want to visit the zones that have the highest number of obstacles.function warningMaker(obstacle) { var count = 0; var zones = []; return function(number, location) { count++; var list = ""; // push an array with location and number zones.push([location,number]); for (var i = 0; i < zones.length; i++) { // replace location and number with appropriate code list += zones[i][0]+ " (" + zones[i][1] + ")" + "\n"; } alert("Beware! There have been " + obstacle + " sightings in the Cove today!\n" + number + " have been spotted at the " + location + "!\n" + "This is alert #" + count + " today for " + obstacle + " danger.\n" + "Current danger zones are:\n" + list); }; }
We already have a list of danger zones, and now the Dev Girls at the Cove want you to add anumberalongside each of the locations.
- Using the
zonesarray, push a sub-array containing both thelocationandnumberfor each obstacle.- Inside the
forloop, find a way to access those values from thezonesarray in order to add them to theliststring.- The final
alertshould be displayed in the following format:Beware! There have beenNote: You do not need to change thesightings in the Cove today! have been spotted at the ! This is alert # today for danger. Current danger zones are: ( ) ( ) ( ) ... alertmessage. You can complete the challenge by updating yourzonesarray and then changing theforloop so that the correct values are added to theliststring.
Final Closed Values II
Now the Dev Girls need each shark to be matched with a corresponding target. A shark’s index in thelistOfSharksarray will match the index of the target that it is supposed to eliminate fromlistOfTargets.Inside themakeTargetAssignerfunction:
- First,
returnan anonymous function that takes in asharkparameter.- Inside the function that is being returned, create a
forloop to loop through thesharksarray.- Inside the loop, find out
ifthe current shark from thesharksarray matches thesharkname that is getting passed as a parameter.- If those values match, build an
alertmessage that produces the following output after calling thegetTargetForfunction:Hey, Ice Teeth! There've been blue witch sightings in our area! Time to take care of business!Note: You do not need to edit the provided function call at the bottom. Just build out themakeTargetAssignerfunction to complete the challenge.function makeTargetAssigner(sharks, targets) {
return function(shark){
for(var i =0;i
if(sharks[i]==shark){
alert("Hey, " + shark + "!\n" +
"There've been " +targets[i]+
" sightings in our area!\n" +
"Time to take care of business!");
}
}
};
}
var getTargetFor = makeTargetAssigner(listOfSharks,
listOfTargets);
getTargetFor("Ice Teeth");


No comments:
Post a Comment