Algorithms: Pairwise

Algorithms: Pairwise
0

#1

Tell us what’s happening:

I cannot pass the test with [1, 1, 1] even though I have a condition that i and y must not be equal. The programme just ignores that part. Could somebody tell me why?

Your code so far


function pairwise(arr, arg) {
  const backupArr = [...arr];
  let newArr = [];

  for (let i = 0; i < arr.length; i++) {
      for (let y = 0; y < arr.length; y++) {
         
          if(i !== y && arr[i] + arr[y] === arg){
            
            
            newArr.push(backupArr.indexOf(arr[i]) + backupArr.indexOf(arr[y]))
            //console.log("A index " + backupArr.indexOf(arr[i]) + " " + backupArr.indexOf(arr[y]));
            //console.log("B value " + arr[i] + " " + arr[y])
            arr.splice(i, 1);
            arr.splice(y-1, 1);
            i--;
            y--;
            }
      } 
  }

let result = newArr.reduce((a,b)=>a + b);
//console.log(result);
return result;
}

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

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/algorithms/pairwise/


#2

I found where the issue was. Please delete this.