हमें एक JavaScript फंक्शन लिखना है जो अंग्रेजी वर्णमाला वाली एक स्ट्रिंग लेता है, उदाहरण के लिए:
const str = 'This is a sample string, will be used to collect some data';
फंक्शन को एक ऑब्जेक्ट रिटर्न करना चाहिए जिसमें स्ट्रिंग में स्वरों और व्यंजनों की गिनती हो, यानी आउटपुट होना चाहिए:
{ vowels: 17, consonants: 29 }
उदाहरण
निम्नलिखिक कोड है:
const str = 'This is a sample string, will be used to collect some data';
const countAlpha = str => {
return str.split('').reduce((acc, val) => {
const legend = 'aeiou';
let { vowels, consonants } = acc;
if(val.toLowerCase() === val.toUpperCase()){
return acc;
};
if(legend.includes(val.toLowerCase())){
vowels++;
}else{
consonants++;
};
return { vowels, consonants };
}, {
vowels: 0,
consonants: 0
});
};
console.log(countAlpha(str));
आउटपुट
यह कंसोल में निम्नलिखिक आउटपुट देगा:
{ vowels: 17, consonants: 29 }