Need some help with the Caesars Cipher challenge

I have, for the most part, created a recursive solution for the challenge. However, when running the code, there is always an undefined that is added to the end of the output. Can someone help me figure out why?

  **My code so far**

function rot13(str) {
for (let i = 0; i < str.length; i++) {
  if (str.length === 0) {
    return "";
  } else if (str.charAt(i).match(/[^A-Z]/)) {
    return str.charAt(i) + rot13(str.substring(i + 1));
  } else if (str.charCodeAt(i) < 78) {
    return String.fromCharCode(str.charCodeAt(i) + 13) + rot13(str.substring(i + 1));
  } else {
    return String.fromCharCode(str.charCodeAt(i) - 13) + rot13(str.substring(i + 1));

  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15

Challenge: Caesars Cipher

Link to the challenge:

it seems that the recursion never enters the first if (the one with the length===0).
I think it’s because, if the length is 0, then the program never enters the for loop.
Try putting the first if statement out of the loop and see if it helps.
Happy coding!

PS: If you use the recursion you don’t really need a for loop since i always stays at 0. I tried removing it (defining var i=0) and it passes.

1 Like

Hey maybe this might help you

javascript - Why does console.log say undefined, and then the correct value? - Stack Overflow.

Thanks! This helped a lot! :grinning:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.