Reputation: 528
I am having problems understanding why a function expression is being made (inside another function) to merely invoke the alert command in the following source code.
Please explain to me in beginner's language. Advanced thank you to every one who contributes.
var parkRides = [ ["Birch Bumpers", 40], ["Pines Plunge", 55], ["Cedar Coaster", 20], ["Ferris Wheel of First", 90] ];
var fastPassQueue = ["Cedar Coaster", "Pines Plunge", "Birch Bumpers", "Pines Plunge"];
function buildTicket (allRides, passRides, pick)
{
if(passRides[0] == pick)
{
var pass = passRides.shift();
// Why does the alert command have to be put inside a function expression? Why do we even need a function expression?
return function()
{
alert("Quick, you have a fast pass to " + pass);
};
}
else
{
for (var i = 0; i < allRides.length; i++)
{
if(allRides[i][0] === pick)
{
// Why is this function being declared again?
return function ()
{
alert("A ticket is printing for " + pick + "!\n" + "Your wait time is about " + allRides[i][1] + " minutes.");
};
}
}
}
}
Upvotes: 0
Views: 78
Reputation: 700562
The function buildTicket
returns a function, and when you call that it will alert the result. You would use it something like this:
// call the function
var f = buildTicket(x, y, z);
// display the result
f();
Upvotes: 3