gpt4 book ai didi

JavaScript - 凯撒密码

转载 作者:行者123 更新时间:2023-12-04 01:46:17 27 4
gpt4 key购买 nike

我知道过去有一些关于 Caesar Ciphers 的帖子,我看过这些帖子,但我还没有找到帮助我解决这个问题的答案,因此我发表了这篇帖子。

语言是 JavaScript。我已经写了 3 个测试,到目前为止有 2 个通过了,但第三个没有通过。我尝试使用嵌套的 for 循环遍历字母和 str,并比较它们,然后根据数字向上/向下移动字母索引,然后将该字母插入一个新数组,并返回连接的数组在最后。

它适用于正数,但不适用于负数。 (我还应该指出,我还没有想过如何处理空格,我只是想先让它对单个单词起作用,然后再从那里开始,谢谢!)

任何指向正确方向的指示都将不胜感激。

套路说明:

函数 caesarCipher 应该接受一个字符串和一个数字 (n),并返回一个应用了凯撒密码的新字符串。凯撒密码将每个明文字母替换为不同的字母,在字母表的上或下固定数量的位置。 N 表示应应用字母表向上或向下移动的次数。它可能是消极的,也可能是积极的。

  E.g.
caesarCipher('hello', 2)
--> 'jgnnq'
caesarCipher('hello world!', -3)
--> 'ebiil tloia!'

我的测试:

const caesarCipher = require("../katas/caesar-cipher");
const { expect } = require("chai");

describe.only("caesarCipher", () => {
it("returns an empty string when passed an empty string", () => {
const alphabet = [
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
"j",
"k",
"l",
"m",
"n",
"o",
"p",
"q",
"r",
"s",
"t",
"u",
"v",
"w",
"x",
"y",
"z"
];
const str = "";
const num = 2;
const actualResults = caesarCipher(alphabet, str, num);
const expectedResults = "";
expect(actualResults).to.equal(expectedResults);
});
it("returns a string with the letters replaced by the number of shifts up the alphabet", () => {
const alphabet = [
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
"j",
"k",
"l",
"m",
"n",
"o",
"p",
"q",
"r",
"s",
"t",
"u",
"v",
"w",
"x",
"y",
"z"
];
const str = "hi";
const num = 2;
const actualResults = caesarCipher(alphabet, str, num);
const expectedResults = "jk";
expect(actualResults).to.equal(expectedResults);
});
it("returns a string with the letters replaced by the number of shifts down the alphabet", () => {
const alphabet = [
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
"j",
"k",
"l",
"m",
"n",
"o",
"p",
"q",
"r",
"s",
"t",
"u",
"v",
"w",
"x",
"y",
"z"
];
const str = "dog";
const num = -3;
const actualResults = caesarCipher(alphabet, str, num);
const expectedResults = "ald";
expect(actualResults).to.equal(expectedResults);
});
});

我的解决方案:

function caesarCipher(alphabet, str, num) {
const strToArray = str.split("");
console.log(strToArray);
const cipheredStr = [];
for (let i = 0; i < strToArray.length; i++) {
for (let j = 0; j < alphabet.length; j++) {
if (strToArray[i] === alphabet[j] && Math.sign(num) === 1) {
console.log(Math.sign(num));
cipheredStr.push(alphabet[(j += num)]);
} else if (strToArray[i] === alphabet[j] && Math.sign(num) === -1) {
console.log(Math.sign(num));
console.log(alphabet[(j -= num)]);
cipheredStr.push(alphabet[(j -= num)]);
}
}
}
console.log(cipheredStr.join(""));
return cipheredStr.join("");
}

结果:

caesarCipher
[]

✓ returns an empty string when passed an empty string
[ 'h', 'i' ]
1
1
jk
✓ returns a string with the letters replaced by the number of shifts up the alphabet
[ 'd', 'o', 'g' ]
-1
g
-1
r
-1
j
jum
1) returns a string with the letters replaced by the number of shifts down the alphabet


2 passing (15ms)
1 failing

1) caesarCipher
returns a string with the letters replaced by the number of shifts down the alphabet:

AssertionError: expected 'jum' to equal 'ald'
+ expected - actual

-jum
+ald

at Context.<anonymous> (spec/caesar-cipher.spec.js:108:30)
at processImmediate (internal/timers.js:456:21)

最佳答案

问题是当 num 为负时,您正在做映射:

cipheredStr.push(alphabet[(j -= num)]);

但是 num 是负数。当您减去一个负数时,您所做的就是加上它的绝对值。

相反,你应该这样做:

cipheredStr.push(alphabet[j + num]);

另外,请注意,要计算字母表中的索引,您不需要将 = 放在那里。


旁注

我知道您的解决方案正在开发中。您必须考虑:

  • 当您对 j + num 求和进行翻译时会发生什么,它超出了您的字母表的边界。 num 的负值也会发生同样的事情。
  • 在问题的声明中,它指出 caesarCipher 应该只接受两个参数,但您将字母表作为第一个参数传递!

祝你的代码好运并继续努力:)

关于JavaScript - 凯撒密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60251108/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com