हमें एक 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 का उपयोग
continue;
};
// यदि हम यहां पहुंचे तो इसका मतलब है कि str[i] और char समान हैं
// इसलिए काउंट बढ़ाएं
count++;
};
return count;
};
console.log(countAppearances(string, 'a'));
console.log(countAppearances(string, 'e'));
console.log(countAppearances(string, 's'));
आउटपुट
कंसोल में निम्नलिखित आउटपुट है -
3 3 4