javascript - Handling elements in arrays differing in their length considering indices of elements -


i have 2 arrays compare, these arrays recreated frequently. arrays have different length, in example below:

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

the following needs done arrays:

a) compare value in origin array value in source array (taking account position through index) , push specially designated array depending on outcome.

in example comparison between 2?1, 4?2 , 3?6 , act accordingly.

this not problem, clarifying this operation in further code example.

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

in example above numbers 4 , 5 in array "unaccounted ones" , need pushed further.

and here have problem, need find way so.

i have suspicion figure out array of 2 larger , loop through , compare values @ same time performing test see if target array value "undefined". if is, push source array value specially designated array.

i have not tried yet aiming to, meanwhile, may there advice on best practices on how achieve this?

what have far follows:

var playerrollslegal = []; var enemyrollslegal = []; var hitstoplayer = []; var hitstoenemy = [];   if (playerrollslegal.length === enemyrollslegal.length) {     (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 enemy");  } else if (playerrollslegal.length < enemyrollslegal.length) {      alert("player has less legal rolls enemy");  } 

answer found.

as expected, test on undefined in target array trick. important though start looping longest array, code sample:

else if (a.length > b.length){      (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]);             }      } } 

Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -