我正在创建一个密码,该密码使用一个字符串并更改每个字母以向前移动3个空格。例如“你好!”会变成“ Khoor!”,但是我遇到了障碍。当人们输入不是字母的内容时,例如“!”或字母之间的空格,我希望能够只返回该值。所以一个“!”会留下一个“!”一个空间将保持一个空间。我想在循环内创建一个if语句来完成此操作,但是如何区分字母和非字母呢?
我的循环是附加的,我希望能够放入一个if语句,这样它将显示为“如果输入为非字母,则返回输入。如果输入为字母,则向前移动3个空格。” >
var 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"];
var string = "Hello World!"
var stringArr = string.split("")
var codedLet = [];
for (var i = 0; i < string.length; i++) {
var input = stringArr[i].toLowerCase()
var codedNum = alphabet.indexOf(input) + 3
codedLet[i] = alphabet[codedNum]
}
答案 0 :(得分:0)
有两种方法:
一种方法是使用[a-zA-Z]{1}
类型的正则表达式,也可以尝试类似的方法
if(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
答案 1 :(得分:0)
如果您需要一个可移动符号的密码,为什么不将所有符号都移动?
请参见下面的代码段
let string = "Hello World!"
const cipher = (s) => [...s].reduce(
(a, l) => a + String.fromCodePoint(l.codePointAt() + 3), '')
const decipher = (s) => [...s].reduce(
(a, l) => a + String.fromCodePoint(l.codePointAt() - 3), '')
let cs = cipher(string)
console.log(cs)
console.log(decipher(cs))