Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!