Javascript String count vowels and consonants

Description

Javascript String count vowels and consonants

'use strict';//w w  w  .  ja v a  2  s .co m

function getCount(words) {
  let myObject = {
    consonants: 0,
    vowels: 0
  };

  for(let i = 0, j = words.length; i < j; i++) {
    if('aeiou'.indexOf(words[i]) !== -1) myObject.vowels++;
    if('bcdfghjklmnpqrstvwxyz'.indexOf(words[i]) !== -1) myObject.consonants++;
  }

  return myObject;
};

console.log(getCount('test'));
console.log(getCount('     '));
console.log(getCount('how are you'));
console.log(getCount('how are you?'));
console.log(getCount(1));



PreviousNext

Related