[SOLVED]Caesars Cipher - Can any one tell me what i am doing wrong in this code?

[SOLVED]Caesars Cipher - Can any one tell me what i am doing wrong in this code?
0

#1

Tell us what’s happening:

Your code so far

function rot13(str) { // LBH QVQ VG!
  
  str=str.split("");
  var x = ["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"];
  
  var j;
  for(var i=0 ;i<str.length;i++){
    
    
    for( j= 0; j <13 ;j++){
    
        if(str[i]==x[j]){
      
         str[i]=x[j+13];
      
          }
     }

     for(j= 13; j <26 ;j++){
    
        if(str[i]==x[j]){
      
         str[i]=x[j-13];
      
          }
     }
    
  }
  
   
  
  return str.join('');
}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36.

Link to the challenge:


#2

your code is fine, but the problem is in your logic, all the characters which are shifted by your first nested loop, also shifted back to the original characters, suppose first nested loop handle “A” character, so it shifted to “N” and when it comes to the condition of the second loop it gets true. so the main thing is that for each character, you should run one loop with checking whether that character comes in the range A-M or N-Z


#3

Just a tip,(although you’ve already almost finished a different way) its easier to use str.charCodeAt(i) in your for loop to get a number, capital letters A-Z correspond to 65-90

var str=ABC;
str.charCodeAt(0) // 65, gives code of the character at index 0 of str

to turn a number back into a capital letter, make sure its within 65-90 and use String.fromCharCode()
example

var str="";
str+= String.fromCharCode(65) // str ="A"

They link to the reference for these in the challenge but they use the mozilla docs, which I find much harder to read than w3schools. (my advice is always check for the methods at w3schools if you cant figure it out from the references they give you on freecodecamp. mozilla will go more in-depth but, its quite hard to understand these docs especially in the beginning)
https://www.w3schools.com/jsreF/jsref_charcodeat.asp

https://www.w3schools.com/jsref/jsref_fromCharCode.asp

Good Luck!


#4

Thanks. I edited the code. It worked . Here is the code

function rot13(str) { // LBH QVQ VG!
  
  str=str.split("");
  var x = ["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"];
  
  var z=[];
  
  var j;
  
  
  for(var i=0 ;i<str.length;i++){
    
    
    for( j= 0; j <13 ;j++){
    
        if(str[i]==x[j]){
      
         str[i]=x[j+13];
      
          z[i]= j ;
          }
     }
      
    if(z[i]==null){
    
     for(j= 13; j <26 ;j++){
    
        if(str[i]==x[j]){
      
         str[i]=x[j-13];
      
          }
     }
    }
  }
  
   
  
  return str.join('');
}

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

#5

hey, I understood that method, i just wanted to solve it of my own :slight_smile:
Here is the new code and it worked perfectly :slight_smile:

Thanks a lot for your help :slight_smile:]

function rot13(str) { // LBH QVQ VG!
  
  str=str.split("");
  var x = ["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"];
  
  var z=[];
  
  var j;
  
  
  for(var i=0 ;i<str.length;i++){
    
    
    for( j= 0; j <13 ;j++){
    
        if(str[i]==x[j]){
      
         str[i]=x[j+13];
      
          z[i]= j ;
          }
     }
      
    if(z[i]==null){
    
     for(j= 13; j <26 ;j++){
    
        if(str[i]==x[j]){
      
         str[i]=x[j-13];
      
          }
     }
    }
  }
  
   
  
  return str.join('');
}

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

#6

I’ve edited both of your posts above for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard.


#7

@kovid927 It will be helpful for others if you add [SOLVED] into your Title.