0

I'm studing pure JS, and get blocked in one excercise. I already resolve it with two loops, but I need to solve using .map by now.

The problem:

A function with two parameters, one parameter is a list of numbers and the second is a numeric value. This function, must print in the console the index of the elements of the array that the sum is the second parameter.

My solution using 2 loops:

 function pairsToSum(numList, sum) {
     const result = [];

     for (i = 0; i < numList.length; i++) {
         for (j = i + 1; j < numList.length; j++) {
             if (numList[i] + numList[j] === sum) {
                 result.push([i, j]);
             };
         };
     };
     return result;
 };

 console.log(pairsToSum([1, 1, 0, 1], 2))   
// [ [ 0, 1 ], [ 0, 3 ], [ 1, 3 ], [ 0, 1 ] ]

But I don't know how to do it using the .map :/

  • 1
    Don't think `.map` would work that well here. Your current code is fine and would probably be the least convoluted, IMO. – CertainPerformance Sep 20 '22 at 02:09
  • `.map` wouldn't work. You should use `.reduce` instead. – Roy Christo Sep 20 '22 at 02:12
  • I agree that `map` isn't the solution, but this can be solved in O(n) by grouping by difference rather than a nested loop. see: [Find all pairs of integers within an array which sum to a specified value](https://stackoverflow.com/questions/1494130/find-all-pairs-of-integers-within-an-array-which-sum-to-a-specified-value) – pilchard Sep 20 '22 at 02:15
  • 3
    Perhaps it's supposed to be solved with a [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map), not `Array.map`? – Nick Sep 20 '22 at 02:27

1 Answers1

-2

could this work?

function pairsToSum(numList, sum) {
        const result = [];

        numList.map((el1, i) => {
          numList.map((el2, j) => {
            if (j > i && el1 + el2 === sum) {
              result.push([i, j]);
            }
          });
        });
        return result;
      }

      console.log(pairsToSum([1, 1, 0, 1], 2));
waiaan
  • 210
  • 1
  • 4
  • 1
    this would work but it's kinda using map while forEach would have been more appropriate. On top of that the complexity is overall bigger than the for loop solution => n2 vs n(n+1)/2. – user3252327 Sep 20 '22 at 02:28
  • @user3252327 it is true, but the poster did not mention that, he seems just want to know how to use .map to solve it. – waiaan Sep 20 '22 at 02:32
  • 1
    I am pretty sure there was a confusion in the terms. When asking to solve the exercise with a map, the examiner/interviewer likely meant to say with a js Map and not with Array.map – user3252327 Sep 20 '22 at 02:36
  • @waiaan this isn't a correct usage of `.map()` though, so we shouldn't be encouraging it to new developers. For more, see [Misusing `map`](https://thenewtoys.dev/blog/2021/04/17/misusing-map/) – Nick Parsons Sep 20 '22 at 04:19
  • Hi Guys! Yes, maybe I make some confusion... I understand the .map in the solution posted by @waiaan, but the answer increases the bigO notation, right? The porpouse is to optmize the bigO notation in this exercise. The examiner said to use map.get(), maybe is in there the confusion. – Ivan Ferreira Sep 20 '22 at 13:12