Caesars Cipher help?

Caesars Cipher help?
0

#1

How to make this code to work?
Please give me some advices so to work this properly!

  for(var i = 0;i<str.length; i++){
    g = str.charCodeAt(i);
    if(g < 65 || g > 90){
      
      x += String.fromCharCode(g);
    }
    if(g < 78){
      if(g > 90){
        
         x += String.fromCharCode((g - 90) + 64);
      }
     x += String.fromCharCode( g + 13);
     
    }else{
    
    x += String.fromCharCode( g - 13);
    }
  }
  
  
  return x.replace(/-|!|\.|\?/gi,'');
}

// Change the inputs below to test
rot13("LBH QVQ VG!");

#2

Please post your entire solution. I can not see the function declaration or if you have anything above the for loop.


#3

Ok i made this solution. But even that i can’t pass the task. Why is that, where im wrong?

function rot13(str) { // LBH QVQ VG!
  var g = ' ';
  var h = ' ';
 var map = Array.prototype.map;
var a = map.call(str, function(x) { 
  return x.charCodeAt(0); 
});
  for (var i in a){
   if(a[i] >= 78 && a[i]<= 90){ 
     g+= a[i] - 13 + ' ';
     
   }
    else if(a[i]<= 78 && a[i] >= 65){
      g+= a[i] + 13 + ' ';
    }
    else{
      
      g+= a[i] + ' ';
    }
  }
   var y =  g.split(' ').slice(1, -1);
  for(var q in y){
  h+= String.fromCharCode(parseInt(y[q]));
   
  }
 
  return h;
}

// Change the inputs below to test
rot13("GUR DHVPX OEBJA QBT WHZCRQ BIRE GUR YNML SBK.");`Preformatted text`

#4

You are very close. Your solution for the example above is producing:

’ THE QUICK BROWN DOG JUMPED OVER THE LAZY FOX.’

and the test case is wanting:

‘THE QUICK BROWN DOG JUMPED OVER THE LAZY FOX.’

You have an extra space in the front of the first word THE


#5

Yes i figured that out when i checked again string’s length. i solve it and pass the task. Thanks for the answer. :slight_smile: