How can a single for loop iterate over multiple arrays?

Abraham Coronado

In this case I'm using two parallel arrays (cost[] and scores[]) both of these with data on them that is parallel to each other.

This code is correct as am copying it from the book I'm using. What i don't get is how can this for loop work for the costs array. I get we are passing both arrays as parameters in the function but in the for loop there is only scores.length, so shouldn't be another loop for cost.lenght?

function getMostCostEffectiveSolution(scores, costs, highScore)  
    var cost = 100;  
    var index;  

    for (var i = 0; i < scores.length; i++) {  
        if (scores[i] == highScore) {  
            if(cost > cost[i]) {
                index = i;  
                cost = cost[i];  
            }
        }
    }
    return index;
}
Patrick Evans

http://en.wikipedia.org/wiki/Parallel_array

In computing, a group of parallel arrays is a data structure for representing arrays of records. It keeps a separate, homogeneous array for each field of the record, each having the same number of elements

If they both are truly parallel than both arrays are going to be the same length.

So scores.length == costs.length. You only need to use one for the loop condition, and use the same index variable to access both arrays.

Example

var a = [1,2,3];
var b = [4,5,6];

for(var i=0; i<a.length; i++){
    console.log(a[i] +"  "+ b[i]);
}

Output:

1 4
2 5
3 6

Using b's length

for(var i=0; i<b.length; i++){
    console.log(a[i] +"  "+ b[i]);
}

Output:

1 4
2 5
3 6

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I generalize this to loop over multiple inputs? Bash arrays?

From Dev

how to iterate over multiple array in loop

From Dev

How can I iterate over multiple arrays and add an exception after every nth iteration?

From Dev

How to iterate over 2 arrays inside a for loop in bash

From Dev

Iterate multiple arrays in single foreach

From Dev

Iterate multiple arrays in single foreach

From Dev

How can I iterate and print over multidimensional arrays in ruby on rails

From Dev

How can I iterate and print over multidimensional arrays in ruby on rails

From Dev

How can I iterate over an array of collections containing arrays in Laravel?

From Dev

Iterate multiple maps with single loop

From Dev

Iterate multiple maps with single loop

From Dev

How to iterate over nested arrays?

From Dev

How to iterate over nested arrays?

From Dev

How do I iterate over multiple, discontinuous ranges in a bash for loop

From Dev

How can I loop through multiple arrays?

From Dev

How to use loop over multiple events in a single text file?

From Dev

Iterate over list of multiple strings using for loop

From Dev

Using a for loop to loop over multiple arrays in bash

From Java

How to iterate over arrays stored in a list and dictionary

From Dev

How to iterate over keys in array of arrays?

From Dev

How to iterate over two arrays in parallel?

From Dev

How to iterate over keys in array of arrays?

From Dev

how to iterate over all strings in a single record

From Dev

How can I convert multiple arrays into one single variable string?

From Dev

PHP How can I iterate over a directory and merge all files that end with same string in a single file of that name

From Dev

How can I convert a folder of audio files into a single file (iterate over many folders)?

From Dev

How to use an async for loop to iterate over a list?

From Dev

How to iterate over multiple slices of a list

From Dev

Can I iterate over two arrays at once in Java?

Related Related

  1. 1

    How can I generalize this to loop over multiple inputs? Bash arrays?

  2. 2

    how to iterate over multiple array in loop

  3. 3

    How can I iterate over multiple arrays and add an exception after every nth iteration?

  4. 4

    How to iterate over 2 arrays inside a for loop in bash

  5. 5

    Iterate multiple arrays in single foreach

  6. 6

    Iterate multiple arrays in single foreach

  7. 7

    How can I iterate and print over multidimensional arrays in ruby on rails

  8. 8

    How can I iterate and print over multidimensional arrays in ruby on rails

  9. 9

    How can I iterate over an array of collections containing arrays in Laravel?

  10. 10

    Iterate multiple maps with single loop

  11. 11

    Iterate multiple maps with single loop

  12. 12

    How to iterate over nested arrays?

  13. 13

    How to iterate over nested arrays?

  14. 14

    How do I iterate over multiple, discontinuous ranges in a bash for loop

  15. 15

    How can I loop through multiple arrays?

  16. 16

    How to use loop over multiple events in a single text file?

  17. 17

    Iterate over list of multiple strings using for loop

  18. 18

    Using a for loop to loop over multiple arrays in bash

  19. 19

    How to iterate over arrays stored in a list and dictionary

  20. 20

    How to iterate over keys in array of arrays?

  21. 21

    How to iterate over two arrays in parallel?

  22. 22

    How to iterate over keys in array of arrays?

  23. 23

    how to iterate over all strings in a single record

  24. 24

    How can I convert multiple arrays into one single variable string?

  25. 25

    PHP How can I iterate over a directory and merge all files that end with same string in a single file of that name

  26. 26

    How can I convert a folder of audio files into a single file (iterate over many folders)?

  27. 27

    How to use an async for loop to iterate over a list?

  28. 28

    How to iterate over multiple slices of a list

  29. 29

    Can I iterate over two arrays at once in Java?

HotTag

Archive