May 20, 2013

what is wrong with my code? it should output 0-9 but it failed to do so. I think the error is in go()

User2384484’s Question:

Can you help me debug this code? I can’t see the errors in it. There is also no ouput to determine the error.

 function go()
{
    var procedures = [];

    for (var i = 0; i < 10; i++) 
    {
        procedures[procedures.length] = function () 
        {
            alert("You are now " + i + " years old");
        } 
        run_procs(procedures);
    }

    function run_procs(procs) 
    {
        for (var i = 0; i < procs.length; i++)
        {
            procs[i]();
        }
    }

    go();
}

You have executed the function inside the function. Put go(); outside.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!