เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่ใช้อาร์เรย์ของสตริง (อาจเป็นอักขระตัวเดียวหรือมากกว่านั้น) ฟังก์ชันของเราควรนับสระทั้งหมดที่อยู่ในอาร์เรย์
ตัวอย่าง
ให้เราเขียนโค้ด -
const arr = ['Amy','Dolly','Jason','Madison','Patricia'];
const countVowels = (arr = []) => {
const legend = 'aeiou';
const isVowel = c => legend.includes(c);
let count = 0;
arr.forEach(el => {
for(let i = 0; i < el.length; i++){
if(isVowel(el[i])){
count++;
};
};
});
return count;
};
console.log(countVowels(arr)); ผลลัพธ์
และผลลัพธ์ในคอนโซลจะเป็น −
10