Hackerrank challenge

Link to the challenge here
Why doesn’t the following work?

function compareTriplets(a, b) {
    const array = [0,0]
   array.forEach(function (item,i) {
     if (a[i] < b[i]) {
       array[1] +=1
     } else if (a[i] > b[i]) {
       array[0] +=1
     } else { array[1] += 0
       
     }
     
   })
  if (a[2] < b[2]) {
       array[1] +=1
     } else if (a[2] > b[2]) {
       array[0] +=1
     } else { array[1] += 0
       
     }
    

return array

}
//console.log(compareTriplets([1,1,1], [2,2,2]))

Why do you have this part of the code here? What does it do?

What does this part do, why do you need it?
What does forEach do?

You may want to review what forEach does, and console.log the parameters of the callback function to know what they are.

You may also want to start writing first pseudocode for the algorithm, to the smallest steps.

1 Like

@newbiewebdeveloper you have to iterate over a or b. not over array.

1 Like

I tried and I solve it!!!
There it’s my js code! lol…

// Complete the compareTriplets function below.
function compareTriplets(a, b) {
    var result = [0, 0];
    for (let i = 0; i < a.length; i++) {
        if (a[i] > b[i]) {
            result[0] += 1;
        } else if (a[i] < b[i]) {
            result[1] += 1;
        } else if(a[i] = b[i]) {
            console.log("don't know what to write in there")
        }
        
    }
    return result;

    
}

This is not a comparison, that is the assignment operator =, you want to use == or ===. Also you could have just used else {...}, or just nothing as you don’t have to do anything in that case

1 Like