私はジャバスクリプトで新しいですし、これは基本的なアルゴリズムScripting.Iからの最後のコースである私は自由なコードのキャンプからフロントエンド開発証明書の次の部分に乗るようコードのこの行を理解しようとしています、私はeveythingを理解したいです。私は解決策を探していて、これを見つけました。私はコメントのいくつかの行を理解していますが、私は式で理解するのが苦労しています、このコードは100%働いていますが、私はちょっとした理解が必要です。ROT13 .charCodeAt()の暗号コード?
function rot13(str) {
//retCharArray is an Array of character codes for the solution
var rotCharArray = [];
//regular expression for all upper case letter from A to Z
var regEx = /[A-Z]/;
//split str into a character array
str = str.split("");
for (var x in str) { //iterate over each character in the array
//regEx.test(str[x]) will return (true or false) if it maches the regEx or not
if (regEx.test(str[x])) {
// A more general approach
// possible because of modular arithmetic
// and cyclic nature of rot13 transform
// I DON'T CLEARLY UNDERSTAND THIS CODE BELOW
rotCharArray.push((str[x].charCodeAt() - 65 + 13) % 26 + 65);
} else {
rotCharArray.push(str[x].charCodeAt());
}
}
//make a string with character codes from an array of character codes
str = String.fromCharCode.apply(String, rotCharArray);
return str;
}
// Change the inputs below to test
rot13("SDASasd");