在嘗試(但失?。閵W丁項目練習編寫凱撒密碼作業后,我最終屈服并查找答案。不過,我不太明白。我正在尋求對每一行的作用及其工作原理的解釋。我復制的代碼對每一行的作用有一些簡短的描述,但我仍然不明白它是如何工作的。const caesar = function (str, amount) { // Wrap the amount if (amount < 0) { return caesar(str, amount + 26); } // Make an output variable var output = ""; // Go through each character for (var i = 0; i < str.length; i++) { // Get the character we'll be appending var c = str[i]; // If it's a letter... if (c.match(/[a-z]/i)) { // Get its code var code = str.charCodeAt(i); // Uppercase letters if (code >= 65 && code <= 90) { c = String.fromCharCode(((code - 65 + amount) % 26) + 65); } // Lowercase letters else if (code >= 97 && code <= 122) { c = String.fromCharCode(((code - 97 + amount) % 26) + 97); } } // Append output += c; } // All done! return output; };
尋求凱撒密碼的解釋
慕俠2389804
2023-10-14 09:54:01