DEV Community

Cover image for Day 7 of JavaScriptmas - Count Vowel Consonant Solution
Sekti Wicaksono
Sekti Wicaksono

Posted on

Day 7 of JavaScriptmas - Count Vowel Consonant Solution

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

Top comments (0)