Caesars Cipher help me please

Caesars Cipher help me please
0

#1

I don’t know what’s wrong with my code. Somehow that returns “THE QUICK BROWN DLOG JUMPEDL OVER THE AZQ FOX.”, but with HUGE spaces beetwen words.

function rot13(str) { 
  str = str.split('');
  var alp = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Q','Z'];
  
  var newStr = "";
  
  for (var i = 0; i < str.length; i++) {       
    for ( var a = 0; a < alp.length ;  a++) {
      
      if (str[i] == " ") {                   // If element is space
        
        newStr += " ";
        
      } else if ( str[i] == alp[a] ) {         // if element is less than 13
        
        if (a < 13) {
          newStr += alp[a+13];
        } else 
          newStr += alp[a-13];
      }
      
    }
    
  }
  return newStr;
}

#2

If you insert a console.log(newStr) at the end of your nestedfor loop as shown below, you will see what is happening.

function rot13(str) { 
  str = str.split('');
  var alp = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Q','Z'];
  
  var newStr = "";
  
  for (var i = 0; i < str.length; i++) {       
    for ( var a = 0; a < alp.length ;  a++) {
      
      if (str[i] == " ") {                   // If element is space
        
        newStr += " ";
        
      } else if ( str[i] == alp[a] ) {         // if element is less than 13
        
        if (a < 13) {
          newStr += alp[a+13];
        } else 
          newStr += alp[a-13];
      }
      console.log(newStr);  // insert this console.log and run your script
    }
    
  }
  return newStr;
}

#3

Thank you, that helped.