Pairwise can't pass one test

Pairwise can't pass one test
0

#1

Tell us what’s happening:
So after a while I have managed to get all but

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

to pass and i just can’t figure out why this one gets 9 instead of 10.
Your code so far

function pairwise(arr, arg) {
  var matches = [];
  var values = [];
  
  function find2dArr(arr, item){
    for(var i=0; i<arr.length; i++){
      if(arr[i][0] === item[0] && arr[i][1] === item[1]){
        return i;
      }
    }
    return -1;
  }
  
  for(var i=0; i<arr.length; i++){    
    for(var j=0; j<arr.length; j++){
      if(i !== j && arr[i] + arr[j] === arg){
        if(!(matches.includes(i) && matches.includes(j))){
          var indices = [JSON.stringify([i,j]), JSON.stringify([j,i])];
          var valOfIndices = JSON.stringify([arr[i],arr[j]]);
          var checkMathes = matches.every((match) => {
            return JSON.stringify(match) !== indices[0] && JSON.stringify(match) !== indices[1];
          });
          var checkValues = values.some((value) => {
            return JSON.stringify(value) === valOfIndices;
          });
          
          
          if(JSON.stringify(matches) === "[]"){
            matches.push([i,j]);
            values.push([arr[i],arr[j]]);
          }else if(checkMathes){
            var valIndex = find2dArr(values, [arr[i],arr[j]]);
            if(checkValues){
              if(matches[valIndex][0] + matches[valIndex][1] > i+j){
                matches[valIndex][0] = i;
                matches[valIndex][1] = j;
              }             
            }else{
              matches.push([i,j]);
              values.push([arr[i],arr[j]]);
            }           
          }
        }  
      }      
    }
  }
  
  matches = matches.reduce((acc, item) => {
    return acc.concat(item);
  }, []);
    
  matches = matches.reduce((acc, item) => {
    return acc + item;
  }, 0);

  return matches;
}

pairwise([0, 0, 0, 0, 1, 1], 1);```
**Your browser information:**

Your Browser User Agent is: ```Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36```.

**Link to the challenge:**
https://www.freecodecamp.org/challenges/pairwise

#2

You are going to have to debug with the console.log statements. I would start near the bottom between the two reduce statements and see if matches looks like you expect. If not, then back up in your code and put console.log statements in to see how your solution arrives at the values in matches.

 console.log(matches);

#3

I started to talk to my brother about it to try to get help from him and then it just like slapped me.
i’m only checking new matches like so

newmatch = [1,2] !== [1,2] or [2,1] 
but not if it is [1,3] << should not match since it reuses 1 right??

#4

Yup that worked :slight_smile: