আমাদের একটি জাভাস্ক্রিপ্ট ফাংশন লিখতে হবে যেটি বাক্যটিতে একটি নির্দিষ্ট অক্ষর কতবার উপস্থিত হচ্ছে তা খুঁজে বের করে
উদাহরণ
চলুন এই ফাংশনের জন্য কোড লিখি -
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){
// using continue to move to next iteration
continue;
};
// if we reached here it means that str[i] and char are same
// so we increase the count
count++;
};
return count;
};
console.log(countAppearances(string, 'a'));
console.log(countAppearances(string, 'e'));
console.log(countAppearances(string, 's')); আউটপুট
নিম্নোক্ত কনসোলে আউটপুট −
3 3 4