Click here to Skip to main content
15,358,946 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
function printOutVowels (str){
var vowels = ["a", "e", "i", "o", "u"]
var letters = str.split("");
var vowelsFound = [];
   for (var i in letters) {
        if (vowels.includes(letters[i])) {
            vowelsFound.push(letters[i]);
        }
    }
    console.log("Vowels:", vowelsFound.join(", "));
}

printOutVowels(("speedkiills"));


What I have tried:

I have tried searching for a solution online
Posted
Updated 26-Jul-21 19:07pm

If you mean "Speedkiills" generates "e,e,i,i" than yes, it should.
If you want to prevent that, then you need to check if the "found vowel" is already in vowelsFound when you find a matching letter and if so, not add it again.
   
Quote:
I have tried searching for a solution online

The first thing to do is to try to understand what is going on, otherwise, searching internet for a solution is expecting to miraculously find an exact solution on internet.
- Forge some sample inputs:
JavaScript
printOutVowels("aeiou");
printOutVowels("uoiea");
printOutVowels("aeiouaeiouaeiou");
printOutVowels("aeiouoiea");
...

You should conclude that the function outputs every vowel in order it is encountered in input. Which is a better definition for what is going on inside.
From this, you have to ask yourself : "How did I told the function that I to output only 1 time each vowel encountered ?"
Then you discover that you didn't told the function only 1 time each vowel encountered.

Advice use positive logic: "My function is doing this, that way.", It should be pretty close to the code.
and now describe the problem : "My function is doing this, that way, but I expect that behavior."
This should lead you an understanding of the problem, and this is a step to solution.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900