Linear execution of code

Hello there, am a javascript newbie and am puzzled by something I’ve never seen in c++ (am used to this).

I have a loop in a function like this:
javascript code:

function fn()
{
    var found = false;
    for(var index = 0; index <= 20; index++)
     {
            another_fn(index,found); //use index
            if(found == true) //editted here, found should be changed within another_fn
                 break;
     }

}

this loop executes 20 times first and then my function is called with the same value of index 20 times, is there a way I could make sure that the function another_fn is called once per an increment of index;

thanks in advance.

It won’t call with the same value for index 20 times, but right now found will never change. it is already being called once for every iteration over index.

For example:

hey, i’ve edited the function see comment, am totally lost why won’t found never update?

Because nothing is every setting found to a new value. You initialized it to false and then check if it’s true.

first of all Thank you for replying, Am also very sorry that i just don’t understand, am sure am not very clear

this is what is happening, another_fn will execute and is the one supposed to change the value of found , so i don’t get how it will not update.

Your function works exactly like i want mine to work but mine doesn’t work.

Scoping. found is passed to another_fn by value, not by reference. I’m rusty on my C++, but I believe that it passes arguments by value as well, which is why you have to use a pointer in order to pass in a shared resource.

https://repl.it/repls/HeftySpitefulQueryplan

There are some freeCodeCamp lessons that teach how scoping works in JavaScript.

Thank you very very much for your help, i understand stuff about references well i’ll check out how to pass references in JS in a moment but for now, there is another bigger problem i wish you help me understand, You see in c++ if i run that code “my first post” the function will be called for every single increment of variable index but in JS for some reason the loop runs to completion first then my function gets called 20 times later, Do you know of anyway i can force this loop to behave similar to the behavior i described in c++.

am using chrome to test a js file am editing, and it’s just mind boggling.

Help me i’ll bless you.

The function is getting called for every iteration of the loop, not 20 times at the end. You can run the first example snippet I posted to see that.

let me test my function again am coming back right now, that the behavior i saw, let me confirm.

Hello i can confirm that is the behavior am getting am not sure why, help please

What makes you think that it is not getting called in every iteration of the for loop?

let me show you my real code, you see i have this confused code i just don’t get it, seriously that is what is happening, i have never seen something like it ever.

function navigateToPageNum(number)
    {   
        container_selector.selectedContainer = number;
        container_selector.updateCollection(100,container_selector.selectedContainer);
		setTimeout(function (){container_selector = angular.element(document.getElementsByClassName('meta-firm-coll')).scope().assetCollection},5000);
    }

function assetOpen()
    {
        if(getNewAsset)
        {
             if(pageIndex == max_index())
            {
                pageIndex = 0;
                assetArray = [];
            }
            
			else
				assetArray = InitStore(); //added this
			
			var found  = false;
			while(pageIndex < max_index() && !found)
			{
				navigateToPageNum(pageIndex);
				//setTimeout(function(){found = open_one_asset();},6000);
				//setTimeout(function(){},10000);
				if(found == true)
				{
					localStorage.setItem("index",pageIndex);
					break;
				}
				alert("current page : "+ pageIndex);
				pageIndex++;
			}
        }
    }

Do you see the code: i’ve commented out the important lines in assetOpen but i just can’t get how the alert() happens 20 times then navigateToPageNum gets called 20 times later, it’s really weird @####

Could that setTimeout in navigateToPageNum cause such behavior, please help

the only thing i need help with is just removing that logical error, where the alert() is called 20 times then my function gets 20 calls later. please anyone?

I think that the nature of alert might be making the timing confusing. Here I have replaced both the call to alert and the body of the function with console logs, so you can see that they are called in sequence. (I had to comment out a bunch of unrelated code to run it).

1 Like