如何在 javascript 的 Caesar Cipher 输入字符串中保留空格?
How to retain spaces in input strings of Caesar Cipher for javascript?
此项目在 javascript。我需要确保输出保留在输入到函数的字符串中找到的 spaces。我试图通过的测试是调用术语“hello world”的函数,移动 13 个字母。从这段代码中,结果是“uryybjbeyq”和“uryyb jbeyq”是预期的。我已经确定我需要一个我已经包含的 if 语句,但不确定我应该在将插入所需 space 的 continue 关键字之前包含什么命令。我是初学者,这只是我的第三个项目,因此我们将不胜感激。请在下面找到相应的代码。
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)
您可以像这样添加白色 space:
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;
}
此项目在 javascript。我需要确保输出保留在输入到函数的字符串中找到的 spaces。我试图通过的测试是调用术语“hello world”的函数,移动 13 个字母。从这段代码中,结果是“uryybjbeyq”和“uryyb jbeyq”是预期的。我已经确定我需要一个我已经包含的 if 语句,但不确定我应该在将插入所需 space 的 continue 关键字之前包含什么命令。我是初学者,这只是我的第三个项目,因此我们将不胜感激。请在下面找到相应的代码。
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)
您可以像这样添加白色 space:
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;
}