কম্পিউটার টিউটোরিয়াল

জাভাস্ক্রিপ্টে সেন্টেন্স-এ নির্দিষ্ট অক্ষর কতবার আসেছে তা বের করা


আমাদের একটি জাভাস্ক্রিপ্ট ফাংশন লিখতে হবে যা বের করবে একটি নির্দিষ্ট অক্ষর সেন্টেন্স-এ কতবার আসেছে।

উদাহরণ

কোডটি নিচে দেওয়া হলো −

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

  1. জাভাস্ক্রিপ্টে সংখ্যার অঙ্কগুলোর যোগফল কতবার করা যায় (Additive Persistence)

  2. জাভাস্ক্রিপ্টে স্ট্রিং-এ নির্দিষ্ট স্ট্রিং-এর আবির্ভাব গণনা

  3. জাভাস্ক্রিপ্টে নির্দিষ্ট অক্ষরে শুরু হওয়া শব্দ খুঁজে বের করা

  4. জাভাস্ক্রিপ্টে স্ট্রিং নির্দিষ্ট বার রিপিট করা