In this article we are going to go over a coding challenge from Codewars.
Problem description
Return a new array consisting of elements which are multiple of their own index in input array (length > 1).
Some cases:
[22, -6, 32, 82, 9, 25] => [-6, 32, 25]
[68, -1, 1, -7, 10, 10] => [-1, 10]
[-56,-85,72,-26,-14,76,-27,72,35,-21,-67,87,0,21,59,27,-92,68] => [-85, 72, 0, 68]
It’s a simple problem, but we’ll spice it up by solving it using 2 different methods:
- With a good ol’ for loop
- With the array’s
filter
method
I’ll leave comments in the code to explain how it works.
The for loop solution
function multipleOfIndex(array) {
// creating a result array where we'll save the needed elements
let res = [];
// loop over the array
for (let i = 0; i < array.length; i++) {
// check if the current element (array[i]) is a multiple of it's index (i) and if it is...
if (array[i] % i === 0) {
// add it to the resulting array
res.push(array[i]);
}
}
return res;
}
The array’s filter method solution
function multipleOfIndex(array) {
// the filter method accepts the 2 arguments that we need:
// 1. the Elements (el)
// 2. the index (idx)
// and returns an filtered array by removing the elements we don't need
return array.filter((el, idx) => el % idx === 0);
}
You can read more about array’s filter method here.
Conclusion
As I said above, it is a pretty simple Coding Challenge.
Let me know if you have another way to solve it! 😄
Happy coding! 😇