# JavaScript Algorithms and Data Structures Projects - Caesars Cipher

Hi! I don`t know why my solution does not process the “N”, it returns undefined instead of A.

Your code so far

``````function rot13(str) {
let alphabet = ["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"];
let newStr = [];
let result = [];
for (let i = 0; i < str.length; i++){
let letter = alphabet.indexOf(str[i]);
if (letter > 13){
let letter2 = alphabet.length - letter;
let rest = 13 - letter2;
newStr.push(alphabet[rest]);
} else if (letter == -1){
newStr.push(str[i])
} else {
newStr.push(alphabet[letter+13]);
}
}
for (let p = 0; p < newStr.length; p++){
result += newStr[p];
}
return result;
}

console.log(rot13("SERR PBQR PNZC")); /*returns FREE CODE CundefinedMP*/
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36`

Challenge: JavaScript Algorithms and Data Structures Projects - Caesars Cipher

Link to the challenge:

Do you know why you are getting `undefined` for `N`? Have you traced it out in your function?

I have no idea why it returns `undefined`. What do you mean by tracing it out, how can I do that?

Walk through your code and see what would happen if the only character passed to the function was `"N"`.

1 Like

Using real inputs in your function to see what values they produce. The main work is done here:

``````        let letter = alphabet.indexOf(str[i]);
if (letter > 13){
let letter2 = alphabet.length - letter;
let rest = 13 - letter2;
newStr.push(alphabet[rest]);
} else if (letter == -1){
newStr.push(str[i])
} else {
newStr.push(alphabet[letter+13]);
}
``````

What is the value of `letter` when you are processing the letter `N`? Which if/else statement will that trigger? Is that the correct one? Do you need to perhaps make an adjustment to your if/else statements to fix this?

1 Like

Ohh, thank you! I had to adjust this if statement, from this:
`if (letter > 13)`
to this:
`if (letter >= 13)`
I was expecting the value of `letter` to be 14 when processing the N, but had forgotten arrays have an index 0
Thank you very much!