Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

240
Vistas
How to return number of vowels in a string in Javascript?

I would be delighted if someone could look into my code. I have been trying to return the total number of vowels in a given string. I want my code to take into account of cases of the letter, any empty spaces in the string and also absence of vowel. I believe there is something wrong with my if-statement which is returning 0 for the string input "Crypt" and "Crypto"

function countVowels(str) {

  let count = 0;
  let arr = str.toLowerCase().split("")
  let vowels = ["a","e","i","o","u"]
  console.log(arr)

  for (let i = 0; i < str.length; i++){
    if (arr[i].includes(vowels)){
      count++
    } else {
      return 0
    }
  }
  return count 

}
console.log(countVowels("Crypto"))
console.log(countVowels("Crypt"))
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

There are several problems with the code you've produced here.

The first is that, for each letter in the string, you're checking whether the letter includes the array of vowels, which will never be true. You've effectively written this...

"a".includes(["a", "e", "i", "o", "u"])

which is backwards.

Instead you need to ask the array whether it includes the given, letter, this way:

["a", "e", "i", "o", "u"].includes("a") // true
["a", "e", "i", "o", "u"].includes("s") // false

The second problem is that, in your else branch, you return 0. This will immediately halt your function and return 0 the first time you encounter a non-vowel. Instead of return 0, you can simply take no action, and drop the else branch entirely.

Ultimately your loop should look like this:

  for (let i = 0; i < str.length; i++){
    if (vowels.includes(arr[i])) {
      count++
    }
  }
about 4 years ago · Juan Pablo Isaza Denunciar

0

In your for loop, the expression:

arr[i].includes(vowels)
// It should be...
vowels.includes(arr[i])

.includes() is a String and an Array method so it can be confusing to a novice. In general, you want to prefix the method with array or string that serves as the filter (the string/array used to compare with the input value).

Also, the else statement will short-circut your function because return ends the whole function immediately. The if statement is sufficient, if there isn't a match then the for loop will just ignore it.

function countVowels(str) {
  let count = 0;
  let arr = str.toLowerCase().split("");
  let vowels = ["a", "e", "i", "o", "u"];
  for (let i = 0; i < str.length; i++) {
    if (vowels.includes(arr[i])) {
      count++
    }
  }
  return count;
}
console.log(countVowels("Crypto"))
console.log(countVowels("Crypt"))

As a more efficient alternative use .filter() like so and return V.length:

const V = array.filter(char => vowels.includes(char));

function countVowels(str) {
  let array = str.toLowerCase().split("");
  const vowels = ["a", "e", "i", "o", "u"];
  const V = array.filter(char => vowels.includes(char));
  return V.length;
};

console.log(countVowels("Crypto"))
console.log(countVowels("Crypt"))

about 4 years ago · Juan Pablo Isaza Denunciar

0

there are a lo of solution to this.

i think you are just starting so i hope following will help

function countVowels(str) {

  let count = 0;
  let arr = str.toLowerCase().split("");
  

  for (let i = 0; i < str.length; i++){
    if (arr[i] === "a" || arr[i] === "e" || arr[i] === "i" || arr[i] === "o" || arr[i] === "u")){
//same as count++ but good practice
     count+=1
    } else {
      return 0
    }
  }
  return count 

}
console.log(countVowels("Crypto"))
console.log(countVowels("Crypt"))
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda