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