Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

163
Visualizações
My code is picking up as if it were all consonant letters

My code is picking up as if it were all consonant letters. Please help me.

function contagem() {
   texto = document.getElementById('ftexto').value;
   vogal = ['a', 'e', 'i', 'o', 'u'];
   consoante = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'];
   totV = 0;
   totC = 0;

   for (x = 0; x < texto.length; x++) {
      if(x == consoante) {
          totC++;
      } else (x == vogal); {
          totV++;
      }
   }

   alert("Essa palavra possui " + totV + " vogais e " + totC + " consoantes.")
}
about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Using match to count letters

You could do this in a more simple way using JavaScript match

Example:

  let toC = text.match(/[bcdfghjklmnpqrstvwxyz]/gi) || [];
  let toV = text.match(/[aeiou]/gi) || [];

Include the letters you want to count inside the brackets []. The /gi instructs match to search the entire text for both lower and upper case letters. And the || [] at the end is the default value when no matches are found.

You can then use toC.length to get the number of matches and toC.join() to get the characters.

Run the code snippet to understand how it works

ftexto.addEventListener("input", e => {

  // count vogal and consoate
  let text = ftexto.value;
  let toC = text.match(/[bcdfghjklmnpqrstvwxyz]/gi) || [];
  let toV = text.match(/[aeiou]/gi) || [];

  message.innerHTML = "Essa palavra possui " + toV.length + " vogais (" + toV.join() + ") e " + toC.length + " consoantes (" + toC.join() + ")";

});
<h4>Digite uma palavra:</h4>
<input id="ftexto">
<h4 id="message"></h4>

about 4 years ago · Santiago Trujillo Relatório

0

To check if a character is vowel or consonant you should not use ==.

It should be checked as the following:

if(consoante.includes(texto[x])){
    totC++;
}
else{
    totV++;
}

here x is the index used to access a particular character from texto

about 4 years ago · Santiago Trujillo Relatório

0

To begin, you should declare your variables using const or let.

This also applies to your for statement when you declare x.

Next, your if statement is checking if x (which is going to be a number, as you've set in your for statement) is equal to consoante, which you've declared as an array of strings.

What you're trying to do is check and see if the character in the string texto at the index x is included in the array consoante. We can accomplish that thusly:

if(consoante.includes(texto[x]))

Finally, since you are checking another condition with your else statement, you need to use else if.

The code below works as expected.

function contagem() {
   const texto = document.getElementById('ftexto').value;
   const vogal = ['a', 'e', 'i', 'o', 'u'];
   const consoante = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'];
   let totV = 0;
   let totC = 0;
   
   for (let x = 0; x < texto.length; x++) {
   if(consoante.includes(texto[x])){
    totC++;
    } else if (vogal.includes(texto[x])) {
      totV++;
    }
 }

   alert("Essa palavra possui " + totV + " vogais e " + totC + " consoantes.")
}

contagem();
<input type="text" id="ftexto" value="abcde"></div>

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda