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 :/