Home > Software design >  vowelFrequency JavaScript function
vowelFrequency JavaScript function

Time:10-06

I need to iterate over an input string and return a string with the count of the frequency of the different vowels in the string. The vowels in the return string should be in the order they appear in the input string. So, if 'hello world' is the function's parameter, the function should return e1o2. The code I have so far is below. It returns e1o2o3. For some reason, it is not stopping the count of o after it hits the o in hello, and seems to be counting the o in world as a separate count. I think it is, finalString = char sum;, that is causing this. But, I do not know how to create this function in the first place. Thank you for your time.

function vowelFrequency(str) {
    let finalString = '';
    let sum = 0;
    for (let char of str) {
        if ('aeiou'.includes(char)) {
            sum  = 1;
            finalString  = char   sum;
        }  
    }
    return finalString; 
};

CodePudding user response:

The main problem is your sum counter. It counts all vowels together.

Better appoach would be to create a dictionary of vowels
where we add 1 every time we met a match.
In short the idea is:

if (char === 'e') { 
    dic['e']  = 1;
}

const text = 'hello world';

function vowelFrequency(str) {
    let finalString = '';
    let dic = {};
    for (let char of str) {
        if ('aeiou'.includes(char)) {
            //check if dictionary has no certain vowel
            //it happens when we first time meet a vowel          
            if (!(char in dic)) {
                dic[char] = 0;
            }
            //increment our counter
            dic[char] =1;
        }
    }
    //by the end of the loop 
    //we have object with { e: 1, o: 2 }
    
    //now we need to gather values into string
    //loop through the object
    for ([char, count] of Object.entries(dic)) {
        finalString  = char   count;
    }
    return finalString;
};

console.log(vowelFrequency(text));


Shorter version of the same solution would be:

function vowelFrequency(str) {
    const vowels = 'aeiou';
    let dic = [...str].reduce((dic, char) => {
        if (vowels.includes(char))
            dic[char] = dic[char]   1 || 1;
        return dic;
    }, {});

    return Object.entries(dic)
        .map(([char, count]) => `${char}${count}`)
        .join('');
};

CodePudding user response:

One concise approach would be to transform the string via String.prototype.replaceAll. The following code searches the original string (which you may wish to normalize beforehand with .toLowerCase() for better results) for any character. This allows us to evaluate every character in the string.

"hello world".replaceAll(/./g, ( char, index, str ) => 
    !'aeiou'.includes( char ) || str.lastIndexOf( char ) > index
        ? "" : char   [ ...str ].filter( o => o == char ).length
);

Each character is checked against a list of vowels. We also check to see if the character index is the last index of this character (does it appear multiple times) in the original string. If either of these conditions fail, an empty string is returned in the character's place.

If our character is in our vowel list, and is the last instance of itself, then we split the original string, filter-out non-matching characters, and return the final count of character instances.

The above approach is somewhat of a gimmick. It's concise, but probably not very self-explanatory or maintainable. Realistically, you'd want to take a slightly more verbose approach (see below).

Note that Map is preferred over a standard object to ensure that key-insertion order is preserved.

function charInstanceString ( input, chars = "aeiou" ) {

    /**
     * Cycle over each character in our string, checking
     * if it appears in our `chars` string. If the character
     * appears in our `chars` string, we'll update our map
     * to reflect the number of instances for the character.
     */
    const charMap = new Map();
    
    for ( const char of input ) {
        if ( !chars.includes( char ) ) continue;
        charMap.set( char, charMap.get( char )   1 || 1 );
    }

    /**
     * Cycle over our map, adding each character (and its
     * corresponding count) to an output string.
     */
    let output = "";
    
    for ( const [ char, count ] of charMap ) {
        output  = `${ char }${ count }`;
    }
    
    return output;
}
  • Related