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 obstacle
encountered. 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 thewarningMaker
function 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 specificnumber
and a specificlocation
to the function, and have those values become part of thealert
message for theobstacle
being passed into thewarningMaker
function.Update thewarningMaker
function 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 thewarningMaker
function.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 anumber
alongside each of the locations.
- Using the
zones
array, push a sub-array containing both thelocation
andnumber
for each obstacle.- Inside the
for
loop, find a way to access those values from thezones
array in order to add them to thelist
string.- The final
alert
should 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: ( ) ( ) ( ) ... alert
message. You can complete the challenge by updating yourzones
array and then changing thefor
loop so that the correct values are added to thelist
string.
Final Closed Values II
Now the Dev Girls need each shark to be matched with a corresponding target. A shark’s index in thelistOfSharks
array will match the index of the target that it is supposed to eliminate fromlistOfTargets
.Inside themakeTargetAssigner
function:
- First,
return
an anonymous function that takes in ashark
parameter.- Inside the function that is being returned, create a
for
loop to loop through thesharks
array.- Inside the loop, find out
if
the current shark from thesharks
array matches theshark
name that is getting passed as a parameter.- If those values match, build an
alert
message that produces the following output after calling thegetTargetFor
function: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 themakeTargetAssigner
function 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