Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript Making a Library to remove vowels from a list of strings

Kyu

New Coder
The code works, but if 2 vowels are next to each other in a word it doesn't remove the second of the two (ex. your becomes yur, I want it to be yr). Also I am using code.org, and it has some functions such as appendItem or removeItem that it uses.

Code:
function removeVowels(list) {
  var filteredList = [];
 
  //goes through each word in list, assigns word to a word in the list
  for (var i = 0; i < list.length; i++) {
    var word = list[i];
    var wordWithoutVowels = "";
    var wordInList = [];
    
    //takes each letter and splits it into a list
    //ex. pizza = ["p", "i", "z", "z", "a"]
    for (var j = 0; j < word.length; j++) {
      appendItem(wordInList, word[j]);
    }
    
    //takes each letter and check if it's a vowel
    //if so, it removes it from the list
    for (var x = 0; x < wordInList.length; x++) {
      if (wordInList[x] == "a") {
        removeItem(wordInList, x);
      } else if (wordInList[x] == "e") {
        removeItem(wordInList, x);
      } else if (wordInList[x] == "i") {
        removeItem(wordInList, x);
      } else if (wordInList[x] == "o") {
        removeItem(wordInList, x);
      } else if (wordInList[x] == "u") {
        removeItem(wordInList, x);
      }
    }
    
    //takes each letter from the list and combines them back into a word
    for (var y = 0; y < wordInList.length; y++) {
      wordWithoutVowels += wordInList[y];
    }
    
    //adds the word without any vowels into filteredList
    appendItem(filteredList, wordWithoutVowels);
    
    //resets the wordWithoutVowels and wordInList variables for the next time the loot repeats
    wordWithoutVowels = "";
    wordInList = [];
  }
 
  //return the filtered list of all the words without any vowels
  return filteredList;
}

var testOne = ["pizza", "fart", "fortnite"];
console.log(removeVowels(testOne));
var testTwo = ["goat", "donkey", "gecko"];
console.log(removeVowels(testTwo));
var testThree = ["nithin", "nathan", "aiden", "damian"];
console.log(removeVowels(testThree));
 
It's easy to see why this does not remove double vowels. For example when x loops through the word "your":
When x=1 you find the letter 'o' and remove it. At that moment, the string becomes "yur". And then you increment x !
So the next letter you see is word[2], that is 'r'. You have neatly skipped over the 'u'.
Removing a vowel at a specified position should be iterated until either the letter at that position is not a vowel, or it is the last letter in the word.
Of course there are other ways to do it. Personally I'd loop through the word and append each non-vowel to another (initially empty) string. Much easier to code and more efficient as well.
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom