Caesars Cipher - problem with for loops


#1

Hello campers,

I am working on Caesars Cipher and I am lost… I need your help. Please, can someone help me? I’l give you special thanks to my #100DaysOfCode log

My code:

function rot13(str) { // 
  var x = 0;
  str = str.split(" ");
  
  for (var arrIndex = 0; arrIndex < str.length; arrIndex++) {
      for (var subArrIndex = 0; subArrIndex < str[arrIndex].length; subArrIndex ++ ) {
         x = str[arrIndex].charCodeAt(subArrIndex) - 13;
        return x;       
 
      }
   
  }
  }

rot13("SERR PBQR PNZC");

It return “70” only… but it should return this array [ [70, 56, 69, 69], [ 67, 53, 68, 69 ], [ 67,65,77,54] ] or something similar to it.

Ok, so look more deep in these for loops.

function rot13(str) { // LBH QVQ VG!
  var x = 0;
  str = str.split(" ");
  
  for (var arrIndex = 0; arrIndex < str.length; arrIndex++) {
      for (var subArrIndex = 0; subArrIndex < str[arrIndex].length; subArrIndex ++ ) {
         x = str[arrIndex].charCodeAt(subArrIndex) - 13;
        console.log (subArrIndex); 
 
      }
   
  }
  }

Return: 0,1,2,3 0,1,2,3, 0,1,2,3

That’s it ? How can I repair it or what I am doing wrong, please?
Or my code is too bad and I should start again from a scratch ?

Lukáš Hanák, Czech Republic.


#2

In the challenge you should work with only Latin letters. You have to detect where first Latin letter starts also where it ends.


#3

Returning x stops the function and returns x, which is 70. Try keeping a variable outside of your loops that holds what info you want and return that variable after the loops are finished.