- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 javascript 新手,这是基本算法脚本的最后一门类(class)。当我从免费代码营获得前端开发证书的下一部分时,我试图理解这行代码,我想了解一切.我一直在寻找解决方案并找到了这个。我理解注释中的一些内容,但我很难理解公式,这段代码 100% 有效,但我只需要进一步理解。这是代码:
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");
最佳答案
上面的两个答案只回答了你的问题的一半,但他们并没有真正看到你的问题,即要回答免费代码营挑战,你必须去理解凯撒密码和ROT13 算术。我也遇到了这个问题。
我们一起来看一下,一步步过一遍。 (我知道并不是每个人都在高中学习过这个 - 我没有!):
// I DON'T CLEARLY UNDERSTAND THIS CODE BELOW
rotCharArray.push((str[x].charCodeAt() - 65 + 13) % 26 + 65);
} else {
rotCharArray.push(str[x].charCodeAt());
}
@bastos.sergio 和 @caulitomaz 的答案都有帮助,但它们仅部分解释,并没有指导您进一步研究:
@bastos.sergio 的
"Get the modulo of 26 (this ensures that the encoding will not encrypt to anything above the 'Z' letter, for instance the 'Z' letter will convert to (90-65+13) % 26 + 65) = 77 'M' Letter. Essentially, it will make the encryption algorithm loop to the beginning."
@caulitomaz 的
In the english alphabet there are 26 characters. That is why you apply the cypher rotation of 13 and then apply the modulo operation.
他们在这里指的是“模块化算术”:
模算术表明,某些数字是“全等的”,也就是说,当对它们应用模运算时,它们共享相同的余数。
例如,2 % 12、14 % 12、26 % 12 = 余数 2。这样,当您的数字必须出现在某个范围内,或者设备只显示某个范围内的数字时,您就可以工作该数字“环回”的位置。
请注意,在我的示例中,使用了 % 12 - 这是 12 小时制时钟可以显示的总小时数。因此,如果现在是凌晨 2 点,并且我想知道 12 小时后的时间,我会添加 12。但是我的时钟无法显示 14:00(不在 12 小时模式下)。 Modulo 12 给我的是真实时间。
示例:现在是凌晨 4 点。我的飞机延误了 29 小时(这是一家糟糕的航空公司)。如何算出新的起飞时间? 4 + 29 % 12 = 上午 9 点。 (如果延迟小于 24,我需要交换 am/pm,但你应该得到图片。)
ROT13
这里我们有一个特殊的情况,即 ROT 13 的旋转密码,其中应用密码两次将得到要编码的原始字符,因为字母表只有 26 个字符,而 13 正好是 26 的一半。
x = ROT13(ROT13(x))
所以实际上,我们在应用或逆向加密时不需要担心加13或减13,我们只需再次应用它即可得到原始数据。
那么,算法如何工作:
正如已经指出的,我们的大写 A-Z 字符代码的范围是 65 (A) 到 90 (Z)。但我们希望使用模运算来找到新字母落在 0-26 范围内的位置。所以从目标字母的字符代码中减去65:str[x].charCodeAt() - 65
应用密码:添加 13
(str[x].charCodeAt() - 65 + 13)
应用模数来查找“循环”的字母。对我们来说是 % 26(字母表中的字符数):
(str[x].charCodeAt() - 65 + 13) % 26
加回 65,这样我们就有了 65...90 范围内的字符代码
(str[x].charCodeAt() - 65 + 13) % 26 + 65)
工作示例:
ROT13(X)
'X'.charCodeAt(0) = 88
- 65 = 23
+ 13 = 36
%26 = 10
+65 = 75
charCodeFrom(75) = K
ROT13(K)
'K'.charCodateAt(0) = 75
- 65 = 10
+ 13 = 23
%26 = 23
+65 = 88
charCodeFrom(88) = X
来源:
http://betterexplained.com/articles/fun-with-modular-arithmetic/ https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/what-is-modular-arithmetic
关于javascript - 来自 .charCodeAt() 的 ROT13 密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36942744/
我有以下内容; function rot13(str) { var result = str.split(""); result.map(function(val) { return v
我知道我可以使用 apply 方法,但我无法理解如何使用它,即括号中的内容。我尝试阅读有关它的文章,甚至观看了一些 YT 视频,但最终还是感到困惑。 我在这里尝试做的是使用 charCodeAt 将以
这怎么可能: var string1 = "🌀", string2 = "🌀🌂"; //comparing the charCode console.log(string1.charCodeAt
为什么代码: ','.charCodeAt(0); 生成 44,即使逗号的字符代码是 188(均由 this table 和在 keyup 中获取 event.keyCode 时 处理程序)。 最佳答
我有这段代码,用于检查右键单击+粘贴输入,然后将每个粘贴字符的字符代码传递到函数中。但是字符代码似乎不正确。我已将“fds”粘贴到输入中,它显示第一个字符的字符代码是102,但“f”的字符代码是70。
我需要一些建议来使用 charCodeAt() 方法。 我的代码如下: doParity() { var str = ["abcd", "efgh", "ijkl"]; var arr = str[0
这个问题以前曾通过不同的方式提出过,但与这个不同。我想通过使用 ES5 或 ES6 编写一个函数来实现,该函数使用系统 A = 1、B = 2、C = 3 等计算得分最高的单词。 该字符串将只包含一个
我试图在数组的特定索引上使用 str.charCodeAt() 。这是不起作用的代码: function rot13(str) { str = str.split(""); for (i =
我的目标是创建一个方法,它接受两个带有特殊字符的字符串并正确地比较它们。我正在努力理解字符编码的逻辑。 所以,我的字符串看起来像: Häzel - This Girl Is Watching Me
这一行,当保存在本地 html 文件中时,警告 229: alert("基".charCodeAt(0)) 但是当警报被复制到 JSFiddle 时,它提供 22522。 为什么会有差异? 最佳答
拿这段代码: var charCode = unkownVariable.charCodeAt(0); charCode 的最大长度是多少?我所有的测试结果都是 2 个字符(2 位数字)。会更长吗?
我正在尝试查找键码(如果这正是我需要的)并将字符更改为递增的键码(这只是为了编码挑战),但它不起作用 function LetterChanges(str) { var newString = "";
Here描述了如何使用 Firebug/FF Web 控制台检查 innerHTML 属性是否可配置和可枚举。如何检查 charCodeAt 函数是否相同? Object.getOwnProperty
对于下面的代码,由于“0”,它显然只会在“man”一词中记录“m”。如何记录所有三个字母? var userChoice = prompt("let me encode something!"); v
我使用以下函数将字符串编码为 base64: encode: function(input) { var output = ""; var chr1, chr2, chr3;
在 JavaScript 中,.charCodeAt() 在您传递给函数的字符串中的某个点返回一个 Unicode 值。如果我只有一个字符,我可以使用下面的代码在 Java 中获取 Unicode 值
这可能是非常明显和简单的东西,以至于我尝试搜索它都失败了,但是:使用 glib 你可以获取 Unicode 的用户输入,类似于我可以粘贴此 亜 的方式这里。我希望能够将其转换为等效的十六进制字符串,例
尝试在 Safari 3 中调用 String.charCodeAt('9') 时,javascript 调试面板出现此错误。 TypeError: Result of expression 'Str
我不确定 0xFF 在这里做什么...它只是为了确保二进制代码是 8 位长还是与有符号/无符号编码有关? ty. var nBytes = data.length, ui8Data = new Uin
我有一个使用非常特定字体的 Unicode 字符。 我需要取那个字符的代码,加上 8,然后把它转换回一个字符串。 这是有效的python代码: char = "" char == chr(ord(
我是一名优秀的程序员,十分优秀!