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");
}