Caesars Cipher The code works but it returns undefined on symbols

Tell us what’s happening:

So the code works but it returns undefined on stuff like space. Any fix for that?
I’m expecting that it is the method “replace” is the cause but I tried to use “letter = …” and it didn’t work as well, a fix please?

Your code so far


function rot13(str) { // LBH QVQ VG!
  var 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"];
  return str.toUpperCase().split("").map((letter,i) => {
    if (/[N-Z]/.test(letter)){
      return letter.replace(/[N-Z]/,Alphabet[Alphabet.indexOf(letter)-13]);
      }
    else if(/[A-M]/.test(letter)){
    return letter.replace(/[A-N]/,Alphabet[Alphabet.indexOf(letter)+13])
    console.log(letter)
  }
  });
  
}

// Change the inputs below to test
rot13("SERR PBQR PNZC");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/caesars-cipher

found the problems

  1. the undefined is returned due the spaces
    whenever the letter is ’ ’ (space) it is returning undefined.
    u can add en else statement to solve this.
    and after this
  2. your program is returning array instead of string so convert it to string and return the final output
1 Like
  1. Yeah, I did that minutes ago.
  2. This is uneeded because I did it on purpose so it can be easier to know the problem, I was planning on fixing this issue first then join them.
    Anyway, thanks.
1 Like