Friday, December 22, 2017

JS Concepts : Rest, Spread & Closures



When we return a function from a function & it is complete with its local variables etc, we call it a closure



function mystery() {
  var secret = 6;
  function mystery2(multiplier) {
    multiplier *= 3;
    return secret * multiplier;
  }
  return mystery2;
}

var hidden = mystery();
var result = hidden(3);


result = 54


function mystery(input) {
  var secret = 5;
  function mystery2(multiplier) {
    multiplier *= input;
    return secret * multiplier;
  }
  return mystery2;
}

var hidden = mystery(4);
var result = hidden(2);


result = 40;


function mystery(input) {
  var secret = 5;
  function mystery2(multiplier) {
    multiplier *= input;
    return secret * multiplier;
  }
  return mystery2;
}

var hidden = mystery(4);
var result = hidden(2);






function mystery(input) {
  var secret = 4;
  input += 2;
  function mystery2(multiplier) {
    multiplier *= input;
    return secret * multiplier;
  }
  return mystery2;
}

function mystery3(param) {
  function mystery4(bonus) {
    return param(6) + bonus;
  }
  return mystery4;
}

var hidden = mystery(3);
var jumble = mystery3(hidden);
var result = jumble(2);


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 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 function

function 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 the warningMaker 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 specific number and a specific location to the function, and have those values become part of the alert message for the obstacle being passed into the warningMakerfunction.
Update the warningMaker function so that it follows the format below. Use the parameters to take the names of the bracketed placeholders:
Beware! There have been  sightings in the Cove today!
 have been spotted at the !
Note: Pay close attention to whitespace! You do not need to call the warningMaker 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.
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.
  1. Using the zones array, push a sub-array containing both the location and number for each obstacle.
  2. Inside the for loop, find a way to access those values from the zonesarray in order to add them to the list string.
  3. The final alert should be displayed in the following format:
Beware! There have been  sightings in the Cove today!
 have been spotted at the !
This is alert # today for  danger.
Current danger zones are:
 ()
 ()
 ()
...
Note: You do not need to change the alert message. You can complete the challenge by updating your zones array and then changing the for loop so that the correct values are added to the list string.
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 II

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:
  1. First, return an anonymous function that takes in a sharkparameter.
  2. Inside the function that is being returned, create a for loop to loop through the sharks array.
  3. Inside the loop, find out if the current shark from the sharks array matches the shark name that is getting passed as a parameter.
  4. If those values match, build an alert message that produces the following output after calling the getTargetFor 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 the makeTargetAssigner 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