ปัญหา
เราจำเป็นต้องเขียนฟังก์ชัน 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