Created a function that is suppose to remove all of the vowels in a given string that is passed as an argument for the function

And yet, everytime I run this code, I get this error:TypeError: str.remove is not a function

I don’t see why this is an error since remove() is a javscript function

Here is the link to my code:https://codepen.io/noblegas/pen/eYmNwBO

1 Like

Unless you create a remove method on the String prototype, there is no such string method named remove. There is string method named replace that might be of use. Make sure to read the documentation to understand how to use it.

If you end up using the replace method, you can actually use a regular expression as the first argument and create a one-line solution for this particular problem.

Would my for loop still work? I want to actually stick to the longer approach first.

If I did use the replace method for my for loop, could I replace my vowels in my string with empty space?

I updated my code and applied your suggestion but now I get this error:

“SyntaxError: invalid regular expression flag e”

Here is the link with my updated code.

Updated my code again , but I don’t get an error ,It just prints the string that I originally passed in my function as an argument but doesn’t removed the vowels.https://codepen.io/noblegas/pen/eYmNwBO

New NEW update: Now I am getting THIS error as it is not removing ALL of my vowels in my string.

“Cllege park sucks”

The specific issue is that the regular expression matches only the first vowel. To solve this problem, you need to learn about flags.

Once you fix this specific issue however, you might realize that the for loop is unnecessary. The string is being re-assigned at every iteration, but it is enough to run the regular expression only once.

Let me know if it helps. I’ll just like to add that the freeCodeCamp curriculum has an entire section devoted to regular expressions, and a one challenge dedicated to this very topic. You will definitely find those resources much more helpful than I could ever be.

1 Like

Can you describe in simple english,
what every line of your code should do?

E.g. why do you use a for loop?

My solution worked and I took your advice:

"function disemvowel(str) {
// for (var x = 0; x < str.length; x++)
// {
// // var c = str.charAt(x);

// // if(c=='a' ||c=='e'||c=='i'||c=='o'||c=='u'){
// //     str.remove(c);

// // }



// }
var regex= /a|e|i|o|u/gi;
var newstr=str.replace(regex, "");
console.log(newstr)

}
console.log(disemvowel(“College park sucks”));"

output of the expression:
“Cllg prk scks”

Thank you so much!

1 Like

Glad you found it helpful @noblegas87 :+1:

@noblegas87 Glad to see you figured out a solution while I was asleep. For the regex version, you could also use a character set.

function disemvowel(str) {
  return str.replace(/[aeiou]/gi, '');
}

If you still wanted to use a for loop, you could do something like:

function disemvowel(str) {
  var strArr = str.split('');
  for (var x = 0; x < str.length; x++) {
    var char = str[x].toLowerCase();
    if (char == "a" || char == "e" || char == "i" || char == "o" || char == "u") {
      strArr[x] = '';
    }
  }
  return strArr.join('');
}

OR

function disemvowel(str) {
  var vowels = ['a', 'e', 'i', 'o', 'u'];
  var strArr = str.split('');
  for (var x = 0; x < str.length; x++) {
    var char = str[x].toLowerCase();
    if (vowels.include(char)) {
      strArr[x] = '';
    }
  }
  return strArr.join('');
}

OR if you do not want to use an extra array to have to join at the end, you can just build a new string:

function disemvowel(str) {
  var vowels = ['a', 'e', 'i', 'o', 'u'];
  var noVowelStr = '';
  for (var x = 0; x < str.length; x++) {
    var char = str[x].toLowerCase();
    noVowelStr += vowels.include(char) ? '' : char;
  }
  return noVowelStr;
}

OR a more functional approach using the filter method:

function disemvowel(str) {
  var vowels = ['a', 'e', 'i', 'o', 'u'];
  return str
    .split('')
    .filter(char => !vowels.includes(char))
    .join('');
}

OR using the reduce method:

function disemvowel(str) {
  var vowels = ['a', 'e', 'i', 'o', 'u', 'z'];
  return str
    .split('')
    .reduce((newStr, char) => newStr += (
      !vowels.includes(char) ? char : ''
    ), '');
}