2

From mozillas js docs:

for (variable of iterable) {
    statement
 }

This is the standard for of statement in js and I want to do something like this:

for (variable of iterable && variable of iterable) {
   statement
}

I just need the loops to not iterate and then iterate again for each nested for of loop. So something that is similar would be great for my needs but the above seems preferable.

I have 2 arrays that I am trying to iterate through and would like to create objects and check for their equality(or something else) further down.

Example scenario:

for (variable1 of iterable1 && variable2 of iterable2) {
   //code
  if (variable1 === variable2){
   //do something
  }
}

my code:

 if (_.intersection(selections[i],questions[i].correctAnswer)) {
       for (const selection of selections[i]) for (const answer of questions[i].correctAnswer) for(const item of questions[i].qPoints) {
          if (selection === answer){

        numCorrect += item;

        console.log(numCorrect)
              }
            }
          }
        }

Basically what I am doing with intersection is checking if the two arrays intersect using underscoreJS. After that, I am trying to create objects for each of these arrays. Firstly, to check whether or not arrays are equal at any point, and if so incrementing numCorrect by the value of the correctanswer at that index(qPoints). The issue is with nested for loops, I am getting the value of qPoints by some multiple(due to excessive iterations).

7
  • What do your iterables look like? Commented Nov 9, 2017 at 23:39
  • They are just arrays of integer values or ints as strings Commented Nov 9, 2017 at 23:40
  • What? Are you trying to compare the nth element of iterable1 with every element in iterable2, or just the nth of iterable1 with the nth of iterable2? Commented Nov 9, 2017 at 23:40
  • If you are working with arrays then forEach or map might be more suited to your needs e.g. iterable1.forEach( function (itemFrom1, i) { var itemFrom2 = iterable2[i]}) Commented Nov 9, 2017 at 23:50
  • @James nth of Iterable1 to nth of Iterable2 Commented Nov 10, 2017 at 0:34

4 Answers 4

2

You don't have to use 'for' - you can access the underlying Iterator directly.

i1 = iterable1.iterator; i2 = iterable2.iterator;
while (!i1.done && !i2.done) {
    variable1 = i1.next; variable2 = i2.next;

    ... do something with the pair
}
Sign up to request clarification or add additional context in comments.

Comments

1

Why not just edit your code to be =>

let correctAnswer = questions[i].correctAnswer; 

let qPoints = questions[i].qPoints;

if (_.intersection(selections[i],questions[i].correctAnswer)) {
   selections[i].forEach((selection, index) => {
     if (selection === correctAnswer[index]) {
       numCorrect += qPoints[index];
      }
     }
    }

Comments

1

I'm not sure what kind of object or shape you got but if your objects are arrays for example, then just loop over one of them and compare by its index to the other array.

const arr1 = [1,2,3,4,5,10,25,45];
const arr2 = [3,5,8,7,5,10,25,84,98,71,1];

arr1.forEach((o,i) => {
  console.log(o,arr2[i],o == arr2[i]);
});

Comments

0

why does something like this not work:

for (var x = 0; x < selections.length && x < questions.length; x++) {
    var selection = selections[x];
    var question = questions[x];
    if (selection === question.correctAnswer) {
        numCorrect += question.qPoints;
    }
}

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.