如何在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
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短