如何将空间保留在凯撒密码的输入字符串中?

发布于 2025-01-25 09:52:49 字数 1431 浏览 2 评论 0原文

这个项目在JavaScript中。我需要确保输出保留输入到功能中的字符串中的空间。我要通过的测试是用13个字母的班次调用“ Hello World”一词的功能。从此代码中,结果是“ Uryyybjbeyq”和“ Uryyb jbeyq”。我已经确定我需要一个我已经包含的if语句,但不确定在继续关键字之前应包含什么命令,以插入所需的空间。我是初学者,这只是我的第三个项目,因此对任何帮助都将不胜感激。请在下面找到相应的代码。

function caesarCypher(string, num){
  // line below is the encrypted string we will return from the function
  const letters = 'abcdefghijklmnopqrstuvwxyz';
  
  let encryptStr = ""
  
  // loop through every character in the inputted string
  for(let i = 0; i < string.length; i++){
  // line below will look up index of char in alphabet  
    let char = string[i];
  /* if statement below is attempting to identify the space in the original string and then add a command that will add a space to the encrypted string then continue to work through the rest of the input */ 
    if (char === " ") {
  //need something to put on this line to insert space;
      continue;
    }

    let index = letters.indexOf(char);
  // index + num below will give us the letter 7 spots over 
    let newIndex = index + num;
  // if statement below makes the function loop back around 
    if (newIndex > 26) {
      newIndex = newIndex - 26;
    }
    
    let newChar = letters[newIndex];
    encryptStr += newChar;
        
  }
    
    return encryptStr;
  
}
/* invoke the function with these parameters to pass the test-- expected result is 'uryyb jbeyq'*/

caesarCypher("hello world", 13)

This project is in javascript. I need to make sure the output retains spaces found in the string that is inputted into the function. The test I am trying to pass is calling the function for the term "hello world" with a shift of 13 letters. From this code, the result is "uryybjbeyq" and "uryyb jbeyq" is expected. I have identified I need an if statement which I have included already, but not sure what command I should include before the continue keyword that will insert the space needed. I am a beginner and this is only my 3rd project so any assistance would be appreciated. Please find the corresponding code below.

function caesarCypher(string, num){
  // line below is the encrypted string we will return from the function
  const letters = 'abcdefghijklmnopqrstuvwxyz';
  
  let encryptStr = ""
  
  // loop through every character in the inputted string
  for(let i = 0; i < string.length; i++){
  // line below will look up index of char in alphabet  
    let char = string[i];
  /* if statement below is attempting to identify the space in the original string and then add a command that will add a space to the encrypted string then continue to work through the rest of the input */ 
    if (char === " ") {
  //need something to put on this line to insert space;
      continue;
    }

    let index = letters.indexOf(char);
  // index + num below will give us the letter 7 spots over 
    let newIndex = index + num;
  // if statement below makes the function loop back around 
    if (newIndex > 26) {
      newIndex = newIndex - 26;
    }
    
    let newChar = letters[newIndex];
    encryptStr += newChar;
        
  }
    
    return encryptStr;
  
}
/* invoke the function with these parameters to pass the test-- expected result is 'uryyb jbeyq'*/

caesarCypher("hello world", 13)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱娆 2025-02-01 09:52:49

您可以添加这样的空白:

encryptStr += " ";

我尝试了您的代码,然后遇到了一个错误...所有字母都是一样的。这是我的做法:

function caesarCypher(string, num){
  let encryptStr = "";
  for(let i = 0; i < string.length; i++){
    let char = string[i];
    if (char === " ") {
        encryptStr += " "; //This adds a white space.
        continue;
    }
    // If you want to keep the case of a letter, skip the 
    // "toLowerCase()" and extend the condition below.
    let asciiCode = string.toLowerCase().charCodeAt(i) + num; 
    if(asciiCode > 122) {
        asciiCode -= 26;
    }
    encryptStr += String.fromCharCode(asciiCode);
  }
  return encryptStr;
}

You can add the white space like this:

encryptStr += " ";

I tried your code and I ran into an error... All letters were the same. Here is how I did it:

function caesarCypher(string, num){
  let encryptStr = "";
  for(let i = 0; i < string.length; i++){
    let char = string[i];
    if (char === " ") {
        encryptStr += " "; //This adds a white space.
        continue;
    }
    // If you want to keep the case of a letter, skip the 
    // "toLowerCase()" and extend the condition below.
    let asciiCode = string.toLowerCase().charCodeAt(i) + num; 
    if(asciiCode > 122) {
        asciiCode -= 26;
    }
    encryptStr += String.fromCharCode(asciiCode);
  }
  return encryptStr;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文