0

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.

7
  • 1
    you need to store the old array1, then compare for getting array2. Commented Aug 9, 2017 at 8:22
  • 1
    xyproblem.info "This didn't work because for loop is async." - it is not. "I couldn't find a proper way to stop forEeach loop" - you can't stop it unless throwing an error. Commented Aug 9, 2017 at 8:22
  • @NickA Yes. When I work on nth element in array1, I will change nth element in array2 to true Commented Aug 9, 2017 at 8:25
  • @jason a concrete example may be helpful, how are you deciding which elements are ready to be updated, what do you mean by "It was modifying all of the array1 elements asynchronously." Commented Aug 9, 2017 at 8:26
  • 1
    Are you perhaps asking how to skip elements you've already modified? I'm unsure about what your code isn't doing right because I don't understand what you're trying to do. Commented Aug 9, 2017 at 8:28

1 Answer 1

2

To solve this kind of issues I think it better to use promises, you want to synchronize multiple asynchronous changes, using promises is easy to find out when all the changes are done.

See the below snippet

var array1 = ["apple", "pear", "cherry", "orange", "mango"];
var array2 = [false, false, false, false, false];
var promises = [];

  for (let i = 0; i < array1.length; i++) {
    let p = new Promise(function(resolve, reject) {
      setTimeout(function() {
        array1[i] += "X";
        array2[i] = true;
        resolve(array1[i]);
      }, randomRange(100,500));
    });
    promises.push(p);
  }

  Promise.all(promises).then(values => {
    console.log(values); 
  });

function randomRange(min, max) {
  return min + Math.floor(Math.random() * (max - min + 1));
}

Sign up to request clarification or add additional context in comments.

3 Comments

Can you explain what ~~ does in your randomRange function?
It is used as a faster replacement for Math.floor().
i will make the changes for a better understanding

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.