javascript counting vowels, consonants and show the occurrance -


i'm having trouble figure out how count vowels, consonants , declare how vowels appears separately, user input. user put text ex: “mischief managed!” , result must be: a: 2 e: 2 i: 2 o: 0 u: 0 non-vowels: 11

var userdata = prompt ("enter text here"); var = 0; var e = 0; var = 0; var o = 0; var u = 0; var consonants = 0; var count;  (count = 0; count <= userdata.legth; count++){    if((userdata.charat(count).match(/[aeiou]/))){             a++;      e++;      i++;      o++;      u++;     }else if((userdata.charat(count).match(/[bcdfghjklmnpqrstvwxyzbcdfghjklmnpqrstvwxyz]/))){      consonants++; } }  console.log ("a: " + a);  console.log ("e: " + e);  console.log ("i: " + i);  console.log ("o: " + o);  console.log ("u: " + u);  console.log ("consonants: " + consonants); 

but it's not working. searched in many other forums but, didn't find this.

firstly, let's point out things

for (count = 0; count <= userdata.legth; count++){ 

length missing letter 'n' , don't need count less or equal because start index 0. need less than.

also:

if((userdata.charat(count).match(/[aeiou]/))){            a++;     e++;     i++;     o++;     u++; } else if((userdata.charat(count).match(/[bcdfghjklmnpqrstvwxyzbcdfghjklmnpqrstvwxyz]/))){     consonants++; } 

what doing here every time matches vowel, increment variable of them, word hi print every vowel had 1 count. take @ one:

var userdata = prompt("enter text here").tolowercase(); var = 0; var e = 0; var = 0; var o = 0; var u = 0; var consonants = 0; var count;  (count = 0; count < userdata.length; count++){     var char = userdata.charat(count);     if(char.match(/[aeiou]/)){         switch (char) {             case 'a':                 a++;                 break;             case 'e':                 e++;                 break;             case 'i':                 i++;                 break;             case 'o':                 o++;                 break;             case 'u':                 u++;                 break;         }     } else if(char.match(/[bcdfghjklmnpqrstvwxyz]/)) {         consonants++;     } }  console.log ("a: " + a); console.log ("e: " + e); console.log ("i: " + i); console.log ("o: " + o); console.log ("u: " + u); console.log ("consonants: " + consonants); 

i following logic keep simple , better readable you. match regular expression same way do, check exact character right now, can increment correct variable.

for else if, in order minimize bit regular expression check if matches 1 of lower case letters, because convert userdata lower case, it:

var userdata = prompt("enter text here").tolowercase(); 

try example , see if fits needs.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -