ปัญหา
เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่รับสตริงของอักขระ str และอักขระตัวเดียว char
ฟังก์ชันของเราควรสร้างสตริงใหม่ที่มีอักขระตัวถัดไปใน str หลังอักขระแต่ละตัว (ถ้ามี)
ตัวอย่าง
ต่อไปนี้เป็นรหัส -
const str = 'this is a string';
const letter = 'i';
const findNextString = (str = '', letter = '') => {
let res = '';
for(let i = 0; i < str.length; i++){
const el = str[i];
const next = str[i + 1];
if(letter === el && next){
res += next;
};
};
return res;
};
console.log(findNextString(str, letter)); ผลลัพธ์
ssn