Free Code Camp Algorithmic Challenges Part 7

This is part 7 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 part 1, part 2, part 3, part 4, part 5, and part 6. You can also look at the github repo with all my solutions here.

Also, I'm 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

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

Caesar's Cipher

Imagine you've got a dial with all 26 letters of the alphabet on it, just like the numbers of a clock. You can use this dial to encode and decode Ceasar's Cipher.

To encode a message, for each letter in the message, move your dial to that letter, then move the dial forward 13 letters, finally write down the new letter.

To decode, just do the reverse. For each letter in the coded message, move your dial to that letter, then move your dial back 13 letters, then write down that letter.

Got it? Now we need to write a function that decodes a Ceasar's Cipher for us.

Here is thine code:

    R = require('ramda')

    decode = c => {
            A = 'A'.charCodeAt(0),
            i = c.charCodeAt(0) - A,
            r = (i + 13) % 26

        return String.fromCharCode(r + A)

module.exports = R.pipe(  
    R.split(''), => c.match(/[A-Z]/g) ? decode(c) : c),

Ignoring the innards of decode for now, we split the string into a character array, map over each character, and then join the array back into a string.

The function that maps over each character uses a regular expression (I promise a regular expression post is coming soon!) to check if the character is something from A to Z.

If it is an alphabetic character we decode it, if not we don't decode it.

Ok, let's talk about how decode works. Each character in JavaScript has an associated code number, which we can get by using the string method charCodeAt.

For example, 'steve'.charCodeAt(2) will get you the char code for the letter 'e' (which is 101), and 'A'.charCodeAt(0) will get you the char code for the letter 'A' (which is 65).

In decode, A is the char code for the letter 'A' and i is the distance from the char code for c to A.

If we move on our dial from c to 13 letters ahead, the distance from that new letter to A is r. So r + A is the character code for that decoded letter, and String.fromCharCode(r + A) gets us that decoded letter.

If that doesn't make sense I think the best thing to do is to look at the code for a bit and maybe re-read this explaination, you'll get it. :)

Sum All Numbers in a Range

Ok, we're to write a function that takes an array of two numbers and returns a sum of all the numbers in between those two numbers (inclusive).

Here is the code for you to look at and understand!!!

module.exports =  
        R.sort((a, b) => a > b),
        ([a, b]) => R.range(a, b + 1),

First I sort the numbers, then I destructure the array to get the two numbers and return a range (the b + 1 is to the range is inclusive). Finally I add up all those numbers with R.sum. I guess I could have used xs => xs.reduce(x, y) => x + y) instead of R.sum but it's the same thing. :)

Simple as cake!

Looking for a software developer?