Count the number of vowels and consonants So I needed to write a function that takes a string and returns a javascript object with vowels and consonants properties that maps to the number of vowels and consonants in the string

function getCount(words) {
  let answer = {vowels: 0, consonants: 0};
  if( typeof words !== 'string') { return answer }

  let vowel  = 'aeiouAEIOU'

  for(char of words){
    if(char === ' ') { continue }
    if(vowel.indexOf(char) === -1){  // not a vowel
      answer.consonants += 1
    }
    else if(vowel.indexOf(char) !== -1){  // is a vowel
      answer.vowels += 1
    }
  }
  return answer
}


Is there a better way of going about it? Preferably a functional approach
This avoids loops completely and expresses your intent more clearly, I think.

Also, note that you should explicitly define the valid consonants, so that you will properly ignore numbers, punctuation, etc.

function getCount(words) {
  var words      = (typeof words == 'string') ? words : '',
      count      = re =&gt; (words.match(re) || []).length,
      vowels     = count(/[aeiou]/ig),
      consonants = count(/[bcdfghjklmnpqrstvxzwy]/ig);

  return {vowels, consonants};
}