I’m not seeing the infinity bug in your program but I will make a suggestion.
I’ve seen a lot of solutions to PairWise (some of them mine) that rely on nested for loops and lots of temporary storage to hold indices, etc. Even when I know that they work it is often hard to wrap my mind around how they work.
I would suggest you consider using some of the built in Array methods. The one suggested in the challenge is .reduce(). That one and maybe a few others would go a long way to eliminating the nested for loops and if statements.
Most of these methods loop through an array on their own without any need for you to write a for loop. Most keep up with the current index and element you are currently operating on for you - no i++, no arr[ i ][ j ]. Readability goes way up so code is easier to understand.
These methods have descriptive names so they are practically self-commenting. Names like .find, .indexOf or .forEach go a long way to clarifying what each part of your function is actually doing.
These do one thing well in a predictable manner. Even if they can’t satisfy your request then they “fail” in a predictable manner too. For example, if .indexOf finds your target it returns an index number, if it cannot, then it returns -1. Simple and easy to test for.
Time spent learning about Array methods (and Object methods and String methods) and callbacks is time well spent.
Here’s the Array method suggested in the challenge - reduce “to reduce it to a single value.” That’s what you want in the end - a single value.
These below you may find of use depending on your logic
The full list of Array methods is on the left of each of these pages. Hover over the links to see a short description of how each one works.
That’s my opinion on the subject. I’m no guru so I’m sharing, not judging. In the end you have to do whatever works for you. Hope this helps you some.