Skip to content

Instantly share code, notes, and snippets.

@rayinla
Last active December 21, 2017 18:44
Show Gist options
  • Save rayinla/438cabe1f531c9ec5041dab936633198 to your computer and use it in GitHub Desktop.
Save rayinla/438cabe1f531c9ec5041dab936633198 to your computer and use it in GitHub Desktop.
function sayMyName(firstName, middleName, lastName){
var praiseList = ["beautiful", "awesome", "splendid", "fantastic", "jaw-dropping"];
//this first closure called combineName() has access to the parameters
//of the outer function and its variable
function combineName(){
var min = 0;
var max = praiseList.length - 1;
//this second getRandomInt() closure has access to the variables in combineName()
function getRandomInt(){
return Math.floor(Math.random() * (max - min) + min);
}
return "Your" + " " + praiseList[getRandomInt()] + " name is " + firstName + " " + middleName + " " + lastName;
}
return combineName();
}
sayMyName("Beyonce", "Giselle", "Knowles-Carter"); // Your beautiful/awesome/splendid... name is Beyonce Giselle Knowles-Carter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment