हमें एक जावास्क्रिप्ट फंक्शन लिखना है जो पता लगाए कि एक विशिष्ट अक्षर वाक्य में कितनी बार आ रहा है।
उदाहरण
इसके लिए कोड:
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