Caesars cipher/Rot-13 Challenge - My solution

Caesars cipher/Rot-13 Challenge - My solution
0

#1

This is my solution to the Caesars Cipher / Rot-13 algorithm challenge.

It takes the given string, and pushes the unicode value of each letter into an array. Then it checks each value in the array for spaces(unicode === 32), A - M ( 65 - 77) and N - Z (78 - 90). It keeps the spaces as is, adds 13 to A - M, and minuses 13 from N - Z. Finally it returns a string from the unicode values.

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

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

#2

Hello, I have moved your post to the algorithm section of the forum as this is not a wiki.


#3

Thanks, I didn’t realize I had posted it as a wiki, my mistake!