如何在JavaScript中将给定字符串N中的每个字母下移到字母表中?
我们得到了一串字母。我们的任务是用与英文字母相距n个字母的字母替换每个字母;
即
如果n=1,则将a替换为b,将b替换为c,以此类推(z将被a替换)。
例如-
const str = "crazy"; const n = 1;
输出应该是-
alphabeticShift(inputString) = "dsbaz".
示例
以下是代码-
const str = 'crazy';
const alphabeticShift = (str = '', n = 1) => {
let arr = [];
for(let i = 0; i < str.length; i++) {
arr.push(String.fromCharCode((str[i].charCodeAt() + n)));
}
let res = arr.join("").replace(/{/g, 'a');;
return res;
};
console.log(alphabeticShift(str));输出结果
以下是控制台上的输出-
dsbaz
热门推荐
1 狗狗技能训练
5 如何训练狗狗笑
6 狗狗训练排便
9 狗狗响片训练
10 如何训练狗狗不捡食
11 狗狗尿训练
12 怎么训练狗狗找东西
13 狗狗训练站立
14 狗狗训练飞盘
15 鞍山狗狗训练
16 怎样训练泰迪狗狗谢谢
17 狗狗外出训练
18 怎么训练金毛狗狗大小便