charAt is not a function Error

charAt is not a function Error
0

#1
function disemvowel(str) {
var splitString = str.split(" "); // splitString is now an Array split into each word
  for (var a = 0; a < splitString.length; a++) {
var vowelStr = splitString[a].split(""); // Splits each word into array
  for (var b = 0; b < vowelStr.length; b++) {
    var isVowel = vowelStr.charAt(b); // Assigning each character to isVowel
    if ( isVowel == 'a' || isVowel == 'e' || isVowel == 'i' || isVowel == 'o' || isVowel == 'u') {
        vowelStr.splice((b-1),0); // If the character is a vowel, remove it
        } else {}
  }
  vowelStr.join();
  }
  splitString.join(" ");
  return splitString;
};

var isVowel = vowelStr.charAt(b); // Assigning each character to isVowel

The highlighted text is what seems to be giving me the error.

My goal with the above code is to write a function that takes a string and return a new string with all vowels removed.

Anyone know what could be causing this issue??


#2

As you already know, the following line of code, vowelStr becomes an array of single characters.

var vowelStr = splitString[a].split(""); // Splits each word into array

Unfortunately, charAt() only works on strings and not arrays, so you could reference the current element of vowelstr which will be a string value and change your chartAt(b) to simply charAt().

Once you fix this above, you are going to still have 2 additional problems to fix:

#1 The first is related to your use of splice. Make sure to review the documentation on what each parameter represents.

#2 You currently are returning splitString at the end, but after you join it with spaces in the line before, it is is just going to be the same value as the original str which was passed into the function to begin with. Within the 2nd for loop you have:

vowelStr.join();

but since you do not assign it to a variable, nothing happens.

Once you get the above to problems fixed, you will still have to figure out the proper way to join each of the words together to return a final string with the correct answer. I will leave that to you to figure out. If you have any questions, please ask.


#3

Thanks for the help. I wasnt aware that charAt() only worked on strings. So it makes sense that i dont need to specify the location of a character if there is only one.


#4

When you finally solve the problem, take a look below at the hidden code for two other solutions for this problem.

You can actually solve this problem without using arrays at all. I modified your code slightly using !== (not equal) and && (logical operator AND) for my if statement condition.

function disemvowel(str) {
  var newStr = '';
  for (var i = 0; i < str.length; i++) {
    var char = str[i];
    if (char !== 'a' && char !== 'e' && char !== 'i' && char !== 'o' && char !== 'u') {
      newStr += char;
    }
  }
  return newStr;
}

OR a very simple version using a regular expression. This version even removes vowels which are capitalized.

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

#5

Thank you so much for helping me out with this. It was much appreciated!