Caesars Cipher need help with my code

Below is my code.i can’t figure out what is wrong here…

function rot13(str) { // LBH QVQ VG!

let arr=[];

let stringval=[];

for(let i=0;i<str.length;i++){

if(str[i]!=' '){


let c=str.charCodeAt(i)-13;



let d=arr[i]+13;




else if(str[i]==" "){

arr[i]=' ';



return arr.join('');


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.


1 Like

Have you been able to work this out? Otherwise I can take a look in a few hours

@leahleen: No,i’m still stuck with this…any help will be greatly appreciated…

you have

let d=arr[i]+13;

where arr[i] is a string and 13 a number, so d is a string and arr[i] will become an empty string

you are only just working with spaces and letters, where in the tests there are some punctuation characters, where those are the corrisponding arr[i] will just be undefined

you will need to fix these, and then console.log() the hell out of each variable to be sure that everything is what you want to be at every step.

An other tool really useful for debugging is this:
it shows step by step what your code does (you can see already your code at this link)

I don’t know if fixing that is enough, but it’s a good step toward make this work