I have an if else statement to match a string with vowels and consonants which works fine. I would like to tidy it up with a switch statement however using match() does not work as a case. what am i missing?
if else //returns vowels: 1, consonants: 3
function getCount(words) {
var v,
c;
if (words === '' || words === ' ') {
v=0;
c=0;
} else if(words.match(/[aeiou]/gi)) {
v = words.match(/[aeiou]/gi).length;
c = words.replace(/\s|\W/g, '').split("").length - v;
} else {
v = 0;
c = 0;
}
return {
vowels: v,
consonants: c
};
}
getCount('test');
switch //returns vowels: 0, consonants: 0
function getCount(words) {
var v,
c;
switch(words) {
case words.match(/[aeiou]/gi):
v = words.match(/[aeiou]/gi).length;
c = words.replace(/\s|\W/g, '').split("").length - v;
console.log("true");
break;
case '':
case ' ':
v = 0;
c = 0;
break;
default:
v = 0;
c = 0;
}
return {
vowels: v,
consonants: c
};
}
getCount('test');