我们需要编写一个包含一串确定字符的函数,并且该函数应返回元音计数与字符串中其他字符和辅音之间的差。
例如-
如果字符串是-
"HEllo World!!"
然后,我们这里有7个辅音,3个元音和其他3个字符,因此输出应为-
|7 - (3+3)| = 1
因此,输出应为1
让我们为该函数编写代码-
const str = 'HEllo World!!';
const findDifference = str => {
const creds = str.split("").reduce((acc, val) => {
let { v, c } = acc;
const vowels = 'aeiou';
const ascii = val.toLowerCase().charCodeAt();
if(!vowels.includes(val.toLowerCase()) && ascii >= 97 && ascii <=122){
++c;
}else{
++v
};
return {c,v};
}, {
v: 0,
c: 0
});
return Math.abs(creds.c - creds.v);
}
console.log(findDifference(str))输出结果
控制台中的输出将为-
1