I have two such arrays :
var array1 = ["apple", "pear", "cherry", "orange", "mango"];
var array2 = [false, false, false, false, false];
When I'm going through array1 , I modify some of its elements. And I turn respective element in array2 to true. For example, if array1 becomes
["apple", "pearX", "cherry", "orange", "mango"]
array2 becomes :
[false, true, false, false, false]
I will not modify "pearX" anymore since it is true in array2. And I should stop modifying array1 when array2 becomes all true. Moreover I can't modify all of array1 at once because "apple"'s value might depend on "pear"'s
For solving this problem, first I made a for loop and put it inside a while loop like this :
var isComplete = false
while(isComplete == false)
{
for(var i = 0; i < array1.length; i++)
{
// do some stuff
if(/*all elements in array2 is true*/)
{
isComplete = true;
break;
}
}
}
This didn't work because for loop is trying to modify all elements at once, it is not waiting for other elements' value to change. It was modifying all of the array1 elements asynchronously.
Then I replaced for loop with array.forEach. But it didn't work because I couldn't find a proper way to stop forEeach loop.
As you can see this problem is not rocket science but I couldn't solve it myself since I'm new to Javascript programming. Can you help me to find a working solution to this problem? Thanks.
array1, then compare for gettingarray2.array1, I will change nth element inarray2totrue