Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i have 2 array
a[]={1,2,3,4,5}
b[]={2,3,4,4,5}
2 arrays are same length
here i have to compare this 2 according to their position,i any position do not match give error unsuccessful
like a[0]=1
b[0]=2
error unsuccessful
no further comparison
Posted
Comments
phil.o 23-Nov-15 7:00am    
What have you tried? That seems so trivial.
Patrice T 24-Nov-15 3:48am    
HomeWork ?
Member 11403530 24-Nov-15 5:26am    
a[]={1,2,3,4,5}
This isn't array. This is array with an object.

use this
var a = [1,2,3,4,5]

That's trivial. Make a loop over the array items, exiting with false at first mismatch. If the loop completes (no mismatches) then return true.
 
Share this answer
 
Comments
Member 11970398 23-Nov-15 7:43am    
can u help me with this
You can try this way :

JavaScript
var  a = new Array(1,2,3,4,5);
           var b = new Array(2,3,4,4,5);
           var errorCounter = 0;
           if (a.length != b.length) { errorCounter = 1 }
           //a.sort();
           //b.sort();
           for (var i = 0, l = a.length; i < l; i++) {
               if (a[i] !== b[i]) {
                   errorCounter++;
               }
           }
           if (errorCounter > 0) {
               alert("Not Same");
           }
           else{
               alert("Same");
           }


[Edit] : commented sorting method.
Good luck.
 
Share this answer
 
v2
Comments
Member 11970398 24-Nov-15 1:02am    
sorry dear ,i want to compare according to their potion value not any where neither sorting
Raje_ 24-Nov-15 2:43am    
Does this now fit your requirement?
for (i=0; i < len; i++) {
if (A[i] !== B[i]) {
o.push({idx: i, elem1: A[i], elem2: B[i]});
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900