JavaScript Algorithms and Data Structures Projects - Caesars Cipher

Tell us what’s happening:
Describe your issue in detail here.
hello so i cant find the problem in this code the thing i know from console is that only the if(alph[x + 13] == undefined) is happening and not the else
Your code so far

function rot13(str) {
  let alph = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  str = str.split("")
  for(let i = 0; i < str.length; i++){
    if(/[a-zA-Z]/g.test(str[i])){
      for (let x = 0; x < alph.length; x++){
        if(str[i] == alph[x]){
          if(alph[x + 13] == undefined){
            str[i] = alph[13 - (alph.length - x)]
          }else{
            str[i] = alph[x + 13]
          }
        }
      }
    }
  }
  console.log(str)
  return str;
}

rot13("SERR CVMMN!");

Your browser information:

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 want to keep searching in this loop after you swapped your letter out?

thank you i was looking at it for at least 3 hours before posting here and your words gave my brain a new prespective and finally saw what console log was showing

added break;

and its rolling nicely

1 Like