# forEach method not cooperating- doesn't go to the last index

Can someone tell me why my forEach function isn’t including each index of ‘arr’? It only seems to be including [0] and [4] …

``````function pairwise(arr, arg) {
let indices = [];
let sum = 0;
let holder = [];

arr.forEach((item)=>{
let x = arr.indexOf(item)
for(var i =0; i<arr.length; i++){
if(item + arr[i] === arg){
indices.push([x, i])
}
}
})

for(var i=0; i < indices.length; i++){
if(holder.indexOf(indices[i][0])<0 && holder.indexOf(indices[i][1])<0){
holder.push(indices[i][0], indices[i][1])
}
}

for(var i =0; i< holder.length; i++){
sum += holder[i]
}

return sum
}

pairwise([0, 0, 0, 0, 1, 1], 1);``````

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (`</>`) will also add backticks around text.

Note: Backticks are not single quotes.

1. forEach has various optional parameters, including the current index. If you use `indexOf` you just they the index of the first element with that value (`arr.indexOf(0)` will always return 0 as the first time the number 0 appear in the array is at index 0)

2. I don’t understand what your code should do. If you want more help you will need to expand on what this algorithm should do.

1 Like

Thanks for the feedback!

I figured it out quickly after reading up on forEach parameters. I just needed to utilize the index parameter to make this work correctly!