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.");
};
}
}
}
}
iis undefined and so it'll return an error...buildTicketis being used to create functions that fire alert messages containing information specific to each item in the loop.returned. Why that is needed we don't know exactly.