Caesars Cipher need help with my code

Caesars Cipher need help with my code
0

#1

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]!=' '){

if(str.charCodeAt(i)>=65&&str.charCodeAt(i)<=90){

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

arr[i]=String.fromCharCode(c);

if(c<65){

let d=arr[i]+13;

arr[i]=String.fromCharCode(d)

}

}}

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

arr[i]=' ';

}

}

return arr.join('');

}

#2

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.


#3

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


#4

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


#5

you have

let d=arr[i]+13;
arr[i]=String.fromCharCode(d)

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: http://pythontutor.com/javascript.html
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