Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Mohammed Jammeh
37,463 PointsJavaScript Caesar Cipher Encryption..
I have a college project that I am working on at the moment but I am stuck on one aspect of the project.
The task is to "provide a Caesar Cipher facility, which would allow a secret agent to encode and decode messages". In one of the lectures, my lecturer mentioned using the .charCodeAt() method but I have never come across this before and checked most tutorials on Treehouse and couldn't find anything either.
After researching online, I now have a brief understanding but I'm not quite sure how to add to my site.
Your help will be really appreciated, thank you :)
1 Answer
Paul Cox
12,671 PointsI'm not sure what part you are struggling with but here is an idea to get you started. Basically you need to iterate through the plaintext, shift each character by the "key" for the caesar cipher, convert that character code back into a character, and then append to the ciphertext.
Note that this is not a complete solution. You will need to consider how to wrap characters (e.g. 'z' -> 'a'), how to deal with upper case characters, and how to populate the plaintext and display the resulting ciphertext.
var plaintext = 'abc';
var ciphertext = '';
var shift = 1;
for (var i = 0; i < plaintext.length; i++)
{
var c = plaintext.charCodeAt(i);
var e = c + shift;
ciphertext += String.fromCharCode(e);
}
console.log(plaintext + ' -> ' + ciphertext);