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