เราต้องเขียนฟังก์ชัน JavaScript ที่หาว่าตัวอักษรเฉพาะปรากฏกี่ครั้งในประโยค
ตัวอย่าง
โค้ดสำหรับนี้:
const string = 'This is just an example string for the program'; const countAppearances = (str, char) => { let count = 0; for(let i = 0; i < str.length; i++){ if(str[i] !== char){ continue; }; count++; }; return count; }; console.log(countAppearances(string, 'a')); console.log(countAppearances(string, 'e')); console.log(countAppearances(string, 's'));ผลลัพธ์
ผลลัพธ์ในคอนโซล:
3 3 4