 # Compare the Triplets script JS

Hello, im trying to take two iteration from array `a1[i] `and` b1[i]` compare with one is greater and then print result to new array as a `+1 `if` true`;
there is one of my attempts:

``````      var a1 = [5,6,7];
var b1 = [3,6,10];
function compareTriplets(arr){
var score = [];
for (var i=0;i<100;i++){
if(a1[i]>b1[i]){score.push(i++)}
if(a1[i]<b1[i]){score.push(i++)}
if (a1[i]==b1[i]) return 0;
}
return score;
}
console.log(a1,b1)
console.log(compareTriplets(a1,b1));
``````

With one of array methods should i use to compare single iterations ?

Hi @Lukasz,

I noticed few things in the code.

1. Parameter Discrepancy
The function has a single argument
``````but while calling the function you send two parameters
``````
1. Not using the parameter `"arr"`in your function. You instead used the variables `a1` and `b1` directly from global scope.

2. I do not quite understand what you want to achieve with this code.

By the above statement, What sort of end array result would you like have?
By my guess, I think your required functionality is –

• Suppose these are your variables `a = [1,2,3] , b = [2,1,3]`
• You compare each of their contents at their respective positions
• You find that `a` is smaller than` b`. Similary, `a > b` and `a = b`
• Thus, the first check would add 1, the next check would add 1 , the last check would add 0 (since `a = b` )
• so your “`score`” variable would be 1+ 1+ 0. i.e. 2
• The function returns 2 as a result .

If the above scenarios are what you’re looking at, then this would be your final code –

``````      var a1 = [5,6,7];
var b1 = [3,6,10];
function compareTriplets( a, b ) {
var score = 0;
for (var i=0; i<a.length ;i++){
if(a[i] > b[i]) {
score = score + 1;
}
if(a[i]<b[i]){
score = score + 1;
}
if (a[i] == b[i]){
score =  score +  0;
}
}
return score;
}
console.log(a1,b1)
console.log(compareTriplets(a1,b1));
``````
1 Like

Thank you mate that helped me a lot.
Thats what i come up with.

``````             var a = [5,6,7];
var b = [3,6,10];
function compareTriplets(a,b){
const { A, B } = a.reduce((acc, cur) => {
const { counter } = acc;
if(a[counter]>b[counter]){
acc.A++;
}
else if(b[counter]>a[counter]){
acc.B++;
}
acc.counter++;
return acc;
},{A: 0, B: 0, counter: 0});
return [A,B];
}
console.log(compareTriplets(a,b));
``````
1 Like