# Covert ForLoop to ForEach in Javascript

I was practicing in Hackerrank JavaScript problems. I found one test which is called `Compare the triplets`. This is the problem:

```a = [1, 2, 3]
b = [3, 2, 1]
For elements *0*, Bob is awarded a point because a[0] .
For the equal elements a[1] and b[1], no points are earned.
Finally, for elements 2, a[2] > b[2] so Alice receives a point.
The return array is [1, 1] with Alice's score first and Bob's second.

```

I found the solution like this:

```let a = [17, 28, 30];
let b = [99, 16, 8];
function compareTriplets(a, b) {
let scoreboard = [0, 0];
for (let i = 0; i < a.length; i++) {
if (a[i] > b[i]) scoreboard[0]++
else if (a[i] < b[i]) scoreboard[1]++
}
return scoreboard
}
compareTriplets(a, b)```

I wanted to convert the ForLoop into `ForEach` method. But I could not find the way to do that.

```let a = [17, 28, 30];
let b = [99, 16, 8];
function compareTriplets(a, b) {
let scoreboard = [0, 0];
a.forEach((element, i) => {
if (element > b[i]) scoreboard[0]++
else if (element < b[i]) scoreboard[1]++
});
return scoreboard
}
compareTriplets(a, b)
```
User contributions licensed under: CC BY-SA
9 People found this is helpful