使用 JavaScript 移动元音和辅音
问题
我们需要编写一个JavaScript函数来接收一串英文字母。我们的函数应该构造一个新的字符串,并且每个辅音都应该在字母表中向前推进9位。如果他们通过'z',则从'a'重新开始。并且每个元音都应该被推到5个位置。
示例
以下是代码-
const str = 'sample string';
const moveWords = (str = '') => {
str = str.toLowerCase();
const legend = 'abcdefghijklmnopqrstuvwxyz';
const isVowel = char => 'aeiou'.includes(char);
const isAlpha = char => legend.includes(char);
let res = '';
for(let i = 0; i < str.length; i++){
const el = str[i];
if(!isAlpha(el)){
res += el;
continue;
};
let pos;
const ind = legend.indexOf(el);
if(isVowel(el)){
pos = (21 + ind) % 26;
}else{
pos = (ind + 9) % 26;
};
res += legend[pos];
};
return res;
};
console.log(moveWords(str));输出结果bvvyuz bcadwp
热门推荐
10 送朋友内裤祝福语简短
11 珠宝顾问生日祝福语简短
12 长辈生日欢快祝福语简短
13 喜欢的人送礼祝福语简短
14 外出生日祝福语简短
15 毕业祝福语贺词大全简短
16 老师迟到生日祝福语简短
17 孩子出院上班祝福语简短
18 酒桌通用祝福语简短