Hello,
My code works, but I am unhappy with how many steps it took me to solve this problem.
Here is the original question:
Create a function divisibleByFivePairSum
that takes an array of numbers.
It should return an array of all the pairs of indices whose sum is a multiple of five.
console.log(divisibleByFivePairSum([1, 5, 2, 0, 4]));
// should return [ [ 0, 4 ], [ 1, 3 ] ]
Here is my code:
function divisibleByFivePairSum(array){
let result = [];
//use a nested loop to iterate through every combo
for (let i = 0; i < array.length; i++) {
for (let j = 0; j < array.length; j++) {
//initialize an empty subArray
let temp = [];
//skip if the same number is selected twice
if (i === j) continue;
//if divisible by 5, push to temp array
if ((array[i] + array[j]) % 5 === 0) {
temp.push(i, j)
//sort temp and push to result
temp.sort((a, b) => a - b);
result.push(temp);
}
}
}
//sort the final result array by using string conversion
result.sort((a, b) => a.toString().localeCompare(b.toString()));
//splice out repeated subArrs, again using string conversion
for (let i = 0; i < result.length - 1; i++) {
if (result[i].toString() === result[i + 1].toString()) {
result.splice(i, 1);
}
}
return result;
}
At first, my code was returning all possible combinations, like this:
[[0,4],[4,0],[1,3],[3,1] ...etc]
I needed to filter out equivalent pair subarrays or simply not push()
them to result
in the first place, but because you can’t just check if array.includes(subArr)
, I had to go through all these steps of saving all possible combinations, sorting them by converting the subArrays to strings, and then looping through and deleting repeated values.
[Another technique I used was using a for (let item of result)
loop inside the main loop to check if item.toString() === temp.toString()
, but that was also bulky and cumbersome.]
This was in an “easy” section, but honestly it gave me some trouble.
Any advice would be appreciated. I feel like this should be solvable in <10 lines.
Thank you.