DEV Community

loading...
Cover image for Day 7 of JavaScriptmas - Count Vowel Consonant Solution

Day 7 of JavaScriptmas - Count Vowel Consonant Solution

Sekti
Keep moving forward. Learn well not learn fast.
・1 min read

Day 7 is counting the sum of vowels and consonant in a single string. A vowel will value of 1 and consonant will give value of 2.

For example, abcde will give value of 8.
a=1, b=2, c=2, d=2, e=1 so 1+2+2+2+1 is 8.

This is the JavaScript solution

function countVowelConsonant(str) {
    let strArray = str.split('');
    let vowels = ['a','i','u','e','o'];

    let result = strArray.reduce((total, letter) => {
         return vowels.includes(letter) ? total + 1 : total + 2;   
    }, 0);

    return result;
}

Enter fullscreen mode Exit fullscreen mode

Discussion (0)