showing results for - "caesars cipher freecodecamp"
Josefina
16 Apr 2020
1const rot13 = str => {
2  let decodedCipher = ''
3  // The number 65 represents A which also is the begining of our alphabets. 
4  // The number 90 represents Z which also is the end of our alphabets.
5  // Space and any other non-alpha character is less than 65(A) and greater than 90(Z).
6  
7  // Split and loop over every character
8  str.split('').forEach(character => {
9    // Get the integer or unicode for ever character which returns a number and store it in letterChar
10    const letterChar = character.charCodeAt()
11    
12    // Check if number(letterChar) is less than 65(A) or greater than 90(Z)
13    // If true, return the number(letterChar) which means, it could be a non-alpha character
14    // If false, return the number(letterChar) + 13, which means it has shifted 13 places.
15    let unicode = letterChar < 65 || letterChar > 90 ? letterChar : letterChar + 13
16
17    // unicode minus 1 is greater or equal to 90(Z)
18    // Set unicode to equal unicode minus 1, 
19    // we minus 1 cause unicode will give us the right operand instead of the left operand
20    // eg N + 13 will give us B instead of A, so,
21    // We substract the now(unicode-1) unicode from 90 to get the number left, then we add it to 65(A),
22    // Cause we start from begining after we've met the end
23    if((unicode - 1) >= 90) unicode = (((unicode - 1) - 90) + 65)
24
25    // Convert and add every character to cipher
26    decodedCipher += String.fromCharCode(unicode)
27  })
28
29  return decodedCipher;
30}
31
32rot13("GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT.");
33
34// With love @kouqhar