Ceasars Cipher - Validating proposed solution

I’m wondering if my proposed code solution for this project will work and if I’m thinking about this correctly.

Step 1: Create an alphabet array:
var alphabetArr = [‘A’,‘B’,‘C’,‘D’,‘E’,‘F’,‘G’,‘H’,‘I’,‘J’,‘K’,‘L’,‘M’,‘N’,‘O’,‘P’,‘Q’,‘R’,‘S’,‘T’,‘U’,‘V’,‘W’,‘X’,‘Y’,‘Z’];

Step 2: Create another where all letters are shifted 13 spaces.
var shiftArr = ['N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M'];
So A <–> N etc…
Step 3:
convert provided string into array.

Step 4:
Create two for loops (one nested) and iterate through alphabetArr and str. If there’s a match, find the index of that letter (so alphabetArr[i]). Since we know that shiftArr represents the letters we need to decode, we can use the index of alphabetArr[i] to find the right letter in shiftArr.

Once all letters are found, convert array into string again.

At a high level, I just want to know if the proposed solution above might work and if I’m thinking about the problem correctly. Looking at the hints provided in the challenge I’m not so sure.

That would probably work, but you’d find a better solution by investigating the helpful links beneath the instructions. Check out this handy list of ASCII codes to find a pattern you could use.

By all means, finish out your thought and try to get it working - functioning code should be your highest priority. Worst case is you can come back to the challenge, implement a better solution and compare them.

1 Like

Thanks @PortableStick! Honestly, I’m having trouble understanding how the links provided in the challenge would be useful in solving the problem.

In other words, how can finding the UTF-Code for every letter in the string help me solve the cipher?

The charCodeA()t method retrieves the UTF code.
The fromCharCode() converts that code to a letter.

Would I just add 13 the return char code number and convert that to a letter using the fromCharCode method?

Perhaps. Check the link I provided and try working through that algorithm on paper with a few different letters.

1 Like

Remember that the shift needs to loop back around - where the letter “after” Z is A. :wink:

1 Like