JavaScript-凯撒密码

时间:2020-02-16 17:21:15

标签: javascript caesar-cipher

我知道过去曾有一些有关Caesar Ciphers的帖子,我曾浏览过,但我没有找到能帮助我解决此问题的答案,因此是我的帖子。

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

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

任何朝着正确方向的指针都会受到赞赏。

卡塔说明:

函数caesarCipher应该采用一个字符串和一个数字(n),并返回一个应用了Caesar密码的新字符串。凯撒密码将每个明文字母替换为一个不同的字母,在字母的上方或下方固定数量的位置。 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)

1 个答案:

答案 0 :(得分:1)

问题是当tableb为负数时,您正在执行映射:

num

但是cipheredStr.push(alphabet[(j -= num)]); 是负数。当您减去负数时,您正在做的就是加上它的绝对值。

相反,您应该这样做:

num

另外,请注意,要计算字母表中的索引,无需在其中放置cipheredStr.push(alphabet[j + num]);


旁注

我了解您的解决方案仍在进行中。您必须考虑:

  • 当您对=求和以进行翻译并且超出字母范围时,会发生什么情况。负值j + num可能会发生同样的事情。
  • 在问题的声明中,它指出num仅应接受两个参数,但是您将字母作为第一个参数传递!

祝您的代码好运,并继续尝试:)

相关问题