Skip to content

Instantly share code, notes, and snippets.

@duggiemitchell
Last active January 20, 2016 15:07
Show Gist options
  • Select an option

  • Save duggiemitchell/1192f3908f7b950fcd2a to your computer and use it in GitHub Desktop.

Select an option

Save duggiemitchell/1192f3908f7b950fcd2a to your computer and use it in GitHub Desktop.
Using a Closure II - CodeSchool JS Roadtrip Part 3
/*The Cove’s Dev Girls just got reports of icebergs in the area!
Build a warning message by passing a "iceberg" obstacle as an argument into the warningMaker function.
Store the results in a new variable called icebergAlert.
Call the icebergAlert function to view the warning message.*/
function warningMaker(obstacle) {
return function() {
alert("Beware! There have been " + obstacle + " sightings in the Cove today!");
};
}
// build your warning message here
var icebergAlert = warningMaker("iceberg");
icebergAlert();
_______
You’ve impressed the Dev Girls with the improved functionality of your warning messages. They’ve built a few warning generators and are ready for any danger that comes up. Suddenly, the radio crackles to life:
Dev Girls, mayday. Dev Girls, mayday!
I’ve got 6 killer penguins on the loose near the "Ice Caves"!
And 1 snow yeti rampaging across the "Blizzard Beach"!
Over and out!
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");
// call the two functions here
killerPenguinAlert(6, "Ice Caves");
snowYetiAlert(1, "Blizzard Beach");
### Modying Bound Values after Closure
/*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.
We already have a list of danger zones, and now the Dev Girls at the Cove want you to add a number alongside each of the locations.
Using the zones array, push a sub-array containing both the location and number for each obstacle.
Inside the for loop, find a way to access those values from the zones array in order to add them to the list string.
Current danger zones are:
<zone1> (<number1>)
<zone2> (<number2>)
<zone3> (<number3>)*/
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);
};
}
## Final Closed Values
Now the Dev Girls need each shark to be matched with a corresponding target. A shark’s index in the listOfSharks array will match the index of the target that it is supposed to eliminate from listOfTargets.
var listOfSharks = ["Sea Pain", "Great Wheezy",
"DJ Chewie", "Lil' Bitey",
"Finmaster Flex", "Swim Khalifa",
"Ice Teeth", "The Notorious J.A.W."];
var listOfTargets = ["icicle bat", "snow yeti",
"killer penguin", "frost tiger",
"polar bear", "iceberg",
"blue witch", "wooly mammoth"];
Inside the makeTargetAssigner function:
First, return an anonymous function that takes in a shark parameter.
Inside the function that is being returned, create a for loop to loop through the sharks array.
Inside the loop, find out if the current shark from the sharks array matches the shark name that is getting passed as a parameter.
If those values match, build an alert message that produces the following output after calling the getTargetFor function:
What up, Ice Teeth!
There've been blue witch sightings in our 'hood!
Time for a swim-by lasering, homie!
function makeTargetAssigner(sharks, targets) {
return function(shark) {
for (var i = 0; i < sharks.length; i++) {
if (sharks[i] == shark) {
alert("What up, " + sharks[i] + "!\n" +
"There've been " + targets[i] +
" sightings in our 'hood!\n" +
"Time for a swim-by lasering, homie!");
}
}
};
}
var getTargetFor = makeTargetAssigner(listOfSharks, listOfTargets);
getTargetFor("Ice Teeth");

The Cove’s Dev Girls just got reports of icebergs in the area!

Build a warning message by passing a "iceberg" obstacle as an argument into the warningMaker function. Store the results in a new variable called icebergAlert. Call the icebergAlert function to view the warning message.

function warningMaker(obstacle) {
  return function() {
    alert("Beware! There have been " + obstacle + " sightings in the Cove today!");
  };
}

// build your warning message here
var icebergAlert = warningMaker("iceberg");
icebergAlert();

You’ve impressed the Dev Girls with the improved functionality of your warning messages. They’ve built a few warning generators and are ready for any danger that comes up. Suddenly, the radio crackles to life:

Dev Girls, mayday. Dev Girls, mayday! I’ve got 6 killer penguins on the loose near the "Ice Caves"! And 1 snow yeti rampaging across the "Blizzard Beach"! Over and out!

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

// call the two functions here
killerPenguinAlert(6, "Ice Caves");
snowYetiAlert(1, "Blizzard Beach");

Modying Bound Values after Closure

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. We already have a list of danger zones, and now the Dev Girls at the Cove want you to add a number alongside each of the locations. Using the zones array, push a sub-array containing both the location and number for each obstacle. Inside the for loop, find a way to access those values from the zones array in order to add them to the list string.

Current danger zones are: () () ()

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

Final Closed Values

Now the Dev Girls need each shark to be matched with a corresponding target. A shark’s index in the listOfSharks array will match the index of the target that it is supposed to eliminate from listOfTargets.

var listOfSharks = ["Sea Pain", "Great Wheezy",
                    "DJ Chewie", "Lil' Bitey",
                    "Finmaster Flex", "Swim Khalifa",
                    "Ice Teeth", "The Notorious J.A.W."];

var listOfTargets = ["icicle bat", "snow yeti",
                     "killer penguin", "frost tiger",
                     "polar bear", "iceberg",
                     "blue witch", "wooly mammoth"];
                     ```
Inside the makeTargetAssigner function:

First, return an anonymous function that takes in a shark parameter.
Inside the function that is being returned, create a for loop to loop through the sharks array.
Inside the loop, find out if the current shark from the sharks array matches the shark name that is getting passed as a parameter.
If those values match, build an alert message that produces the following output after calling the getTargetFor function:
What up, Ice Teeth!
There've been blue witch sightings in our 'hood!
Time for a swim-by lasering, homie!

function makeTargetAssigner(sharks, targets) { return function(shark) { for (var i = 0; i < sharks.length; i++) { if (sharks[i] == shark) { alert("What up, " + sharks[i] + "!\n" + "There've been " + targets[i] + " sightings in our 'hood!\n" + "Time for a swim-by lasering, homie!"); } } }; }

var getTargetFor = makeTargetAssigner(listOfSharks, listOfTargets); getTargetFor("Ice Teeth");


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment