How to get number of vowels in JavaScript [closed]

2

How can I get the number of uppercase and lowercase vowels in a variable in JavaScript.

        var person = new Object();
        person = prompt("Introduce tus datos");

        //Numero de vocales
        var numVocales;

        for (i = 0; i < 10; i++) { 

            var vocal;

            if (i = 0){
                vocal = "a";
            }
            else if (i = 1){
                vocal = "e";
            }
            else if (i = 2){
                vocal = "i";
            }
            else if (i = 3){
                vocal = "o";
            }
            else if (i = 4){
                vocal = "u";
            }
            else if (i = 5){
                vocal = "A";
            }
            else if (i = 6){
                vocal = "E";
            }
            else if (i = 7){
                vocal = "I";
            }
            else if (i = 8){
                vocal = "O";
            }
            else if (i = 9){
                vocal = "U";
            }

            var aux = person.indexOf(vocal);

            if(aux != -1){
                numVocales++;
            }

        }


        alert(numVocales);

I can not show the information in the alert, it may be that I have badly raised the for. Also I do not know if using indexOf () I get the first letter that concide.I mean that if I have ana, using indexOf () I would return one or two?

    
asked by Eduardo 18.10.2017 в 16:18
source

2 answers

5

Pablo Lozano's solution will serve you more because it is a more complete analysis of your code. I am going to give you a solution that is much shorter than what you are trying to do.

The idea is to use regular expressions to find the occurrences of vowels ( [aeiou] ) within the string. You find them all by the g modifier and it is not case sensitive by the i modifier.

Here is an example:

var cadena = "aAeEiIoOuU son vocales";
var numeroVocales = cadena.match(/[aeiou]/gi).length;

console.log(numeroVocales);
    
answered by 18.10.2017 / 17:03
source
5

The code has a lot of flaws, so I try to list them and leave you the task of fixing it:

var person = new Object();

This first line does not help you, you can eliminate it, because in the next line you assign a new value to person

person = prompt("Introduce tus datos");

//Numero de vocales
var numVocales;

for (i = 0; i < 10; i++) { 

This loop does not make much sense: up to 10 because you have 10 possible letters to search? What you have to do is go through the string that is in person .

    var vocal;

    if (i = 0){

This is an error that often goes unnoticed by newbies: instead of using == (compare if they are equal) you used an assignment. That what it does is that i is worth 0 and that that is the value to check. Since 0 is "false", it passes to the next if ...

        vocal = "a";
    }
    else if (i = 1){

Here again we have the same error, assign 1 to i, with the difference that 1 is "true" and the comparisons are over. This means that in each iteration i is worth 1 and the loop never ends, hanging the execution. That's why you never reach alert .

Track to search for vowels:

let vocales='aeiouAEIOU'; 
let letras='ca';
if (vocales.indexOf(letras.charAt(0) != -1) { }
    
answered by 18.10.2017 в 16:50