0

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.");    
                    };
                }
            }
        }
    }
7
  • What are you asking? In the first case the variable i is undefined and so it'll return an error... Commented Apr 18, 2014 at 18:07
  • Oops! I edited the source code now. Please take a look, Ian. Commented Apr 18, 2014 at 18:10
  • it means that he wants to return a function where an alert command is inside. for what thats useful i cant say because i dont see the context. if you do not put it in the function the difference would be that it would directly alert by execution buildticket function, and like its here it wont alert it only returns a function Commented Apr 18, 2014 at 18:10
  • To piggy back off of Thorsten's comment, in order to really grasp this, you need to understand that a function in JavaScript can return another function that has access to the data inside the function that created it. That concept is called a "closure". buildTicket is being used to create functions that fire alert messages containing information specific to each item in the loop. Commented Apr 18, 2014 at 18:13
  • 1
    The point is that the function expression is not called, but returned. Why that is needed we don't know exactly. Commented Apr 18, 2014 at 18:14

1 Answer 1

3

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.