영어 알파벳이 포함된 문자열을 받는 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 }