हमें एक जावास्क्रिप्ट फ़ंक्शन लिखना है जो एक स्ट्रिंग लेता है जिसमें अंग्रेजी वर्णमाला होती है, उदाहरण के लिए -
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 }