Ceasar cipher - Here's my code - spoiler alert

Ceasar cipher - Here's my code - spoiler alert
0

#1

Can’t seem to find the FreeCodeCamp Bot that has answers and explanations, so I’m just going to post my code.

function rot13(str) { // LBH QVQ VG!
var newString = ‘’;

for (var i =0; i<str.length; i++){
var n = str.charCodeAt(i);
var r = (n + 13 +65)%26 + 65;

if (/[A-Z]/.test(str[i])){
     newString += (String.fromCharCode(r));
    } else {
  newString += str[i];
}   

}
return newString;
}


#2

We can provide answers, but what is your question?


#3

This is what I came up with…

function rot13(str) { // LBH QVQ VG!

  function ceaser(num){
    if (num < 65 || num > 90) {
      return num;
    }
    if (num < 78){
      return num + 13;
    }
    else {return num - 13;}
  }
  
  var phrase = "";
  for (var i = 0; i < str.length; i++) {
    var num = ceaser(str[i].charCodeAt());
    var letter = String.fromCharCode(num);
    phrase = phrase.concat(letter);
  }
  return phrase;
}

#4

Here’s mine :slight_smile:

function rot13(str) { // LBH QVQ VG!
var newStr = [];
for (var i = 0; i < str.length; i++){
if (str.charCodeAt(i) < 65) {
newStr.push(str.charCodeAt(i));
}else if (str.charCodeAt(i) > 64 && str.charCodeAt(i) < 91){
if ((str.charCodeAt(i) + 13) > 90){
newStr.push(((str.charCodeAt(i) + 13) - 90) + 64);
}else {
newStr.push(str.charCodeAt(i) + 13);
}
}
}
return String.fromCharCode.apply(null, newStr);
}

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


#5

Here’s mine code:

function rot13(str) { // LBH QVQ VG!
  var newArr = str.toUpperCase().split('');
  var z = "";
  
  for (var i = 0; i < newArr.length; i++) {
    z = newArr[i].charCodeAt();
    
    if (z > 64 && z < 78) {
      newArr[i] = String.fromCharCode(z + 13);
    } else if (z > 77 && z < 91) {
      newArr[i] = String.fromCharCode(z - 13);
    }
    
  }
  
  return newArr.join('');
}

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

#6

It actualy very interested to see difference, who measuring speed and how it possible to do for JS scripts?
here my code

> function rot13(str) {
    var nStr="";
	var fStr="";
	for(var i=0;i<=str.length-1;i++)
	{
	nStr=str.codePointAt(i);
		if (nStr>=65&&nStr<=77)
		{
		nStr=String.fromCharCode(nStr+13);
		}else if (nStr>=78&&nStr<=91){
		nStr=String.fromCharCode(nStr-13);
		}else{
		nStr=String.fromCharCode(nStr);
		}
    fStr+=nStr;
	}
  return fStr;
}

#7

Here’s my code. It’s a bit long but it also does the job.

function rot13(str) { // LBH QVQ VG!
var obj = {
A: “N”, B: “O”, C: “P”, D: “Q”, E: “R”, F: “S”, G: “T”, H: “U”, I: “V”, J: “W”, K: “X”, L: “Y”, M: “Z”, N: “A”, O: “B”, P: “C”, Q: “D”, R: “E”, S: “F”, T: “G”, U: “H”, V: “I”, W: “J”, X: “K”, Y: “L”, Z: “M”};
var fare = str.split(’’);
var fada = [];
var i = 0;
while (i < fare.length) {
if (fare[i] === “!” || fare[i] === “?” || fare[i] === “.” || fare[i] === " "){
switch (fare[i]) {
case “!”:
fada.push(fare[i]);
break;
case “?”:
fada.push(fare[i]);
break;
case “.”:
fada.push(fare[i]);
break;
case " ":
fada.push(fare[i]);
break;
}
}

  else {
      fada.push(obj[fare[i]]);
}
i++;  

}

return fada.join(’’);
}

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