Hi @Lukasz,
I noticed few things in the code.
-
Parameter Discrepancy
The function has a single argument
but while calling the function you send two parameters
-
Not using the parameter "arr"
in your function. You instead used the variables a1
and b1
directly from global scope.
-
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[0]
is smaller than b[0]
. Similary, a[1] > b[1]
and a[2] = b[2]
- Thus, the first check would add 1, the next check would add 1 , the last check would add 0 (since
a[2] = b[2]
)
- 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));