Has anyone else struggled with Caesars Cipher project in Javascript?

Link: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/caesars-cipher

I don’t know what it is but it’s actually starting to demotivate me significantly. Been making good progress up until now but I have literally been stuck on this project for a week now, been dedicating an hour a day to trying to solve it and each time I just end up coming unstuck.

This is not a hard algorithm by any means, but it might need knowledge of some techniques. Can you describe your strategy, like what steps it could be broken into?

it may be time to step back from the computer, pick pen and paper and try to come up with an algorithm that you can follow on paper, once you can manually do all the steps and get the correct output it could be right time to convert it to paper

or viceversa, pick one of your tries, and follow the values in the code step by step with pen and paper

1 Like

I have tried breaking the string down into an array containing individual characters and then looping through the array comparing the indexed letter with an ordered array of alphabetical characters and then subtracting or adding 13 to the index to decipher it, that worked well but I couldn’t figure out a way to handle whitespaces and symbols.

I’ve then moved onto to try charAt and fromCharAt, I can get the charAt value but I always get stuck when trying to convert the UTF-16 decimal value back to a string using fromCharAt().

You can skip the characters you don’t care about in the loop (if the character is a-z, do thing, otherwise don’t).

The method to convert code to string is String.fromCharCode():

It can be a challenging project, if you try to jump right into coding.

For me, and especially with this one, I tried to “abstract” my thinking. I tried to come up with some pseudocode for a “shift the alphabet by some number offset”. Rather than focusing on the thirteen characters, or one character, or seven… what would be the process, if you were offsetting by N characters?

I find it much easier to sit down with a pad or a whiteboard, and draw the thing out. When I was a kid, secret decoder rings were a thing. Loved my secret decoder ring! Miss when Crackerjack prizes were actual prizes…

Why do I bring that up? The ring was a wheel, the alphabet was on both the inner wheel, and the plastic base that wheel spun on. By moving that inner wheel, I could offset a code by that many letters. The alphabet comes back around, so ‘a’ follows ‘z’ on both alphabet rings. Does that idea help? Well, for me, yes. It led me to this pseudocode:

 * Given a string, and an offset "distance" (how far a letter has shifted from 
 *   its original position), here's the steps to "encode" that string:
 * 1. Before we start with the string, get the ASCII code for 'a', and 'A'. These will
 *    be the starting index of any alphabet code.
 * 2. Break the string into an array of single characters.
 * 3. For each letter, there's a few steps:
 *    a. If this is NOT a letter ([a-z] or [A-Z]), simply store it as-is.
 *    b. Get the ASCII code for this letter.
 *    c. add our offset to that code.
 *    d. We *could* have a problem here, we might go right off the end of the
 *       alphabet. Suppose our letter was 'y', and our offset was 13 - we just shot
 *       right past 'z' and into uncharted waters! What can we do?
 *       i.  if our letter code is less than the ascii code for either 'a'+26 or 'A'+26,
 *           depending on the case of this letter, then we're in range. Convert it 
 *           back to a letter, and store this!
 *       ii. If our letter code is NOT less, then we can shift it *back* in range by
 *           subtracting 26 from it (the length of our entire alphabet range). Then we
 *           convert it back, and store this one.
 * 4. Having gone through each character, simply glue them back into a string
 *    and return that string! Boom done.

Note that there isn’t a bit of code there (although, because I do know about ASCII, I do reference that). There are code guidelines, in a wording that makes sense to me. Each bit that needed to be broken down further, I did so.

Often, those areas that got broken down further are pretty indicative of a code block. That might be an if(...){...} expression, or an inner function, or something. But it isn’t code. Its what my code might look like if I converted it back to “people-speak”.

This is a practice I use often, when I face a challenge I’m not sure of. Or sometimes I’ll use this approach to get a different handle on a problem I think I know, and I’ll realize I was totally off-base.

It is natural to struggle with some of these challenges, it’s both learning a new language, and learning a different way to think. It takes time, and practice, and frustration. It’s learning to play an instrument, and learning to read music, at the same time.

But practice, and patience, and asking good questions. There are many of us who have gone through these same issues, and who have plowed face-first into these same concrete walls. We’d love to see you get through, and we believe that you can. If we didn’t, we wouldn’t spend time here helping others along.

The struggle is real, sure. You can master this, and when you do, you can take pride in building your own mental map, just a little bigger.


Thanks for all your help and encouragement guys, been a tough week but I have finally managed to pass the project.

Full process and solution here: Caesars Cipher Project Help

Ultimately took a similar approach to you snowmonkey, just coded one step at a time until I was getting near enough the end product then spent some time debugging until the code functioned as needed. It’s by no means the cleanest or most efficient code but it works and I’m happy with that at this stage :joy:


A post was split to a new topic: Need help with Caesar’s Cipher project