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