เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่รับสตริงที่อาจมีตัวอักษรบางตัว ฟังก์ชันควรนับและส่งคืนจำนวนสระที่มีอยู่ในสตริง
ตัวอย่าง
ต่อไปนี้เป็นรหัส -
const str = 'this is a string';
const countVowels = (str = '') => {
str = str.toLowerCase();
const legend = 'aeiou';
let count = 0;
for(let i = 0; i < str.length; i++){
const el = str[i];
if(!legend.includes(el)){
continue;
};
count++;
};
return count;
};
console.log(countVowels(str)); ผลลัพธ์
ต่อไปนี้เป็นผลลัพธ์บนคอนโซล -
4