আমাদের একটি জাভাস্ক্রিপ্ট ফাংশন লিখতে হবে যা স্পেস ধারণকারী একটি স্ট্রিং নেয়। ফাংশনটি কেবলমাত্র সেই স্ট্রিংটিতে উপস্থিত স্থানের সংখ্যা গণনা করা উচিত।
যেমন −
যদি ইনপুট স্ট্রিং −
হয়const str = 'this is a string';
তারপর আউটপুট −
হওয়া উচিতconst output = 4;
উদাহরণ
const str = 'this is a string';
const countSpaces = (str = '') => {
let count = 0;
for(let i = 0;
i < str.length; i++){
const el = str[i];
if(el !== ' '){
continue; };
count++; };
return count;
};
console.log(countSpaces(str)); আউটপুট
এটি নিম্নলিখিত আউটপুট −
তৈরি করবে4