Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does this function change the order of the array items?

The function should return the earliest complete pair that equals the sum (s);

function sumPairs(ints, s, arr = ints.slice(), result) {
  let lastIndex = arr.length;
  for (let int of arr) {
    for (let i = 1; i < arr.length; i++) {
      if (int + arr[i] === s && i < lastIndex) {
        result = [int, arr[i]];
        lastIndex = i;
      }

    }
   arr.splice(0, 1); 
  }
  return result;
}

  console.log(sumPairs([10, 5, 2, 3, 7, 5], 10)); // [3,7] is expected
  // console.log(sumPairs([1, 2, 3, 4, 1, 0], 2));
  // console.log(sumPairs([0, 0, -2, 3], 2));
  // console.log(sumPairs([1, 4, 8, 7, 3, 15], 8));
   console.log(sumPairs([1, -2, 3, 0, -6, 1], -6)); // [0,-6] is expected
  // console.log(sumPairs([4, 3, 2, 3, 4], 6));

For the examples that are returning oddly, I have listed the expected results next to the function call/console log.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

By splicing inside the loop, you’re removing one element from the beginning of the array while you’re iterating over the array. This results in the int variable being iterated over being only every other value, eg:

sumPairs([10, 5, 2, 3, 7, 5], 10)
          ^^     ^     ^
function sumPairs(ints, s, arr = ints.slice(), result) {
  let lastIndex = arr.length;
  for (let int of arr) {
    console.log(int);
    for (let i = 1; i < arr.length; i++) {
      if (int + arr[i] === s && i < lastIndex) {
        result = [int, arr[i]];
        lastIndex = i;
      }

    }
    arr.splice(0, 1);
  }
  return result;
}

sumPairs([10, 5, 2, 3, 7, 5], 10)

So the first item in the returned array is never an odd index.

Why splice? It’s not doing anything for you at all, and doesn’t make sense for the algorithm.

To return the first pair that exists, construct a Set of the numbers needed to be found to sum to the desired amount. For example, when iterating over 3 with a target of 10, add 7 to the Set.

function sumPairs(ints, target) {
  const set = new Set();
  for (const num of ints) {
    if (set.has(num)) return [target - num, num];
    set.add(target - num);
  }
}

console.log(sumPairs([10, 5, 2, 3, 7, 5], 10)); // [3,7] is expected
console.log(sumPairs([1, 2, 3, 4, 1, 0], 2));
console.log(sumPairs([0, 0, -2, 3], 2));
console.log(sumPairs([1, 4, 8, 7, 3, 15], 8));
console.log(sumPairs([1, -2, 3, 0, -6, 1], -6)); // [0,-6] is expected
console.log(sumPairs([4, 3, 2, 3, 4], 6));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading