Free Code Camp Algorithmic Challenges Part 9

This is part 9 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. :)

Here are the links for parts 1, 2, 3, 4, 5, 6, 7, and 8. You can also look at the github repo with all my solutions.

I'm also using whatever libraries I feel like using (just ramda in this post) and any new JavaScript features I like for all of these challenges, something you can't do at freecodecamp.com.

With all that out of the way, Let's get on to the next challenges. :)

Wherefore art thou

This time we need to make a function that takes an array of objects and another object. The function has to return a filtered version of the array. Elements pass the filter if each key value pair of the object are also present in the element.

For example: If the function is given [{a: 1, b:2}, {a: 4, b: 5, c:5}, {y: 33, a: 1, b: 2}] and {a: 1, b: 2} it would return [{a: 1, b:2}, {y: 33, a: 1, b: 2}].

Codey code code code!

const  
    R = require('ramda')

const  
    keyMatch = x => y =>
        R.pipe(
            R.keys,
            R.all(key => x[key] === y[key]))
                (x)

module.exports = (xs, y) =>  
    xs.filter(keyMatch(y))

It's just a simple filter. keyMatch grabs all the keys from its first argument and checks to see if those key value pairs match for its second argument.

Search and Replace

This is a simple string search and replace with one twist: if the word you're replacing is capitalized, then the new word needs to be capitalized as well.

Code!!

const  
    isCapitalized = str =>
        str.charAt(0).match(/[A-Z]/),
    capitalize = str =>
        str.charAt(0).toUpperCase()
            + str.slice(1)

module.exports = (str, before, after) =>  
    str.replace(
        before,
        isCapitalized(before)
            ? capitalize(after)
            : after)

The function we use has a little ternary if. If before is capitalized, then we capitalize after, otherwise not.

isCapitalized Just checks to see if the first letter of str matches characters A through Z (regex post coming soon I swear!).

capitalize takes the first character of its string, turns it to upper case, then adds the remaining string using slice just like we would with an array.

Pig Latin

You know what pig latin is! Here, we're just asked to piglatin-ify a single word.

Here's the code:

const  
    consonantPig = R.pipe(
        R.match(/([^aeiou]+)(.*)/),
        R.tail,
        R.reverse(),
        R.join(''),
        str => str + 'ay'),
    vowelPig = str => str + 'way'

module.exports = str =>  
    str.charAt(0).match(/[aeiou]/)
        ? vowelPig(str)
        : consonantPig(str)

str.charAt(0).match(/[aeiou]/) just checks to see if the first letter of str is a vowel. If it is, we use vowelPig, if not we use consonantPig.

vowelPig just adds 'way' to the end of our string.

consonantPig uses a regex to split the first consonant group (the first few letters in a string before the first vowel) from the rest of the string.

Because R.match gives us an array holding more than just those two strings we want (i.e. the first consonant group and the rest of the word) we use R.tail to get rid of the first element in the array.

We then reverse the array, join it up into a single string, and tack 'ay' onto the end of it.

That Seems like enough for this post. See you next time and happy coding!

Looking for a software developer?