MY #100 DAYS OF CODE CHALLENGE JOURNEY-DAY 6

in #coding6 years ago

IMG_20190128_180911.jpg

Problem: Caesars Cypher ROT13.

Problem Definition: Caesars Cypher ROT13 decodes word by shifting it thirteen places backward.

Algorithm

  1. The given string is splitted into an array/list of characters.

  2. I looped through each character and get its ASCII code.

  3. I then checked if each character ASCII code is between A-Z and return the character as it is if it's not between A-Z .

  4. I shifted character code of character whose ASCII code fall between A-Z by 13 and return the corresponding character at that position.

  5. For character with ASCII code lesser than 78, I shifted them forward by 13 places so as to fall between A-Z .

JavaScript Code

function ROT13(str) {

return str.split(" ").map.call(str, function(char){
x = char.charCodeAt(0);

  if (x < 65 || x > 90) {
    return String.fromCharCode(x); 
  }
  
  else if (x < 78) {
    return String.fromCharCode(x + 13);
  }
 
  return String.fromCharCode(x - 13);

}).join(" ");

}

Coin Marketplace

STEEM 0.22
TRX 0.20
JST 0.034
BTC 98934.25
ETH 3347.90
USDT 1.00
SBD 3.08