0

I have 2 arrays to compare, these arrays are recreated frequently. Very often the arrays will have different length, as in example below:

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

The following needs to be done with the arrays:

a) compare value in a origin array with the value in the source array (taking into account position through index) and push it to a specially designated array depending on the outcome.

in my example i do comparison between 2?1, 4?2 and 3?6 and act accordingly.

This is not a problem, I am mostly clarifying this as this operation will be in the further code example.

b) push whatever remains (unaccounted values) to a specially designated array.

In the example above the numbers 4 and 5 in array a would be the "unaccounted ones" and need to be pushed further.

And here I have a problem, as I need to find a way to do so.

I have a suspicion that I could figure out which array of the two is larger and then loop through it and compare values at the same time performing a test to see if a target array value is "undefined". If it is, then push the source array value to a specially designated array.

I have not tried it yet but aiming to, meanwhile, may be there is some advice on best practices on how to achieve this?

What I have so far is as follows:

var playerRollsLegal = [];
var enemyRollsLegal = [];
var hitsToPlayer = [];
var hitsToEnemy = [];


if (playerRollsLegal.length === enemyRollsLegal.length) {
    for (var m = 0; m < playerRollsLegal.length; m++){
            if (playerRollsLegal[m] === enemyRollsLegal[m]) {
                hitsToPlayer.push(enemyRollsLegal[m]);
                hitsToEnemy.push(playerRollsLegal[m]);
            }
            else if (playerRollsLegal[m] > enemyRollsLegal[m]) {
                hitsToEnemy.push(playerRollsLegal[m]);
            }
            else if (playerRollsLegal[m] < enemyRollsLegal[m]) {
                hitsToPlayer.push(enemyRollsLegal[m]);
            }

    }
}
else if (playerRollsLegal.length > enemyRollsLegal.length){ 
    alert("Player has more legal rolls than enemy");

}
else if (playerRollsLegal.length < enemyRollsLegal.length) { 
    alert("Player has less legal rolls than enemy");

}
Vadimster
  • 119
  • 9
  • Do you want to implement difference on arrays? If so check this thread http://stackoverflow.com/questions/1187518/javascript-array-difference and have a look at the most voted answer. – cristian Dec 16 '14 at 11:42
  • @Octopus-Paul Not really. I need to compare elements by their indices between 2 arrays and whatever is left (e.g. target array is larger) should be pushed to another array. The suggested approach would in my example give me 1,3,5,6 whereas I need only 4 and 5 to be left (as they cannot be compared to array b). – Vadimster Dec 16 '14 at 12:00

1 Answers1

0

Answer found.

As expected, test on undefined in the target array does the trick. It is important though to start looping from the longest array, code sample:

else if (a.length > b.length){ 
    for (var u = 0; u < a.length; u++){
        if (b[u] === undefined) {
            hitsToEnemy.push(a[u]);
        } 
        else if (a[u] > b[m]){
            hitsToEnemy.push(a[u]);
        }
        else if (a[u] < b[u]) {
                hitsToPlayer.push(b[u]);
            }
     }
}
Vadimster
  • 119
  • 9