This is part 6 of a series in which I give solutions to the Free Code Camp Algorithmic challenges. Don't look at this post until you've solved these challenges yourself! You've been warned. :)
Ok, with all that out of the way, Let's get on to the next challenges. :)
Here-a is-a the code-a:
const R = require('ramda') module.exports = R.filter(x => x)
So we need to make a function that takes an array as its first argument, and then any number of other arguments. We need to return a copy of the array whose elements do not include any of those other arguments.
f([1, 2, 3, 4, 5], 2, 4, 5) would return
[1, 3], and
f(['hey', 'you', 'are', 'a', 'hamster', 'man'], 'hey', 'man') would return
['you', 'are', 'a', 'hamster'].
module.exports = (xs, ...ys) => xs.filter(x => !ys.includes(x))
I talk about the
... operator here, but basically it makes
xs the given array and turns the remainder of the arguments into another array,
ys. Now all we do is filter out those elements in
xs included in
Nexty next next!
We need to make a function that takes an array of numbers
xs and a number
y. The function needs to sort
xs then figure out which index to insert y into xs that will keep the resulting array sorted.
module.exports = (xs, y) => xs .sort((a, b) => a > b) .filter(x => x < y) .length
First we sort the array, then we remove all numbers from the array greater than or equal to y, finally the length of the resulting array will be the same as the index we needed to insert
xs at (e.g. one index more than the last index of the filtered array).
Ok, that's it for now. See you next week!