Home > Enterprise >  My code is picking up as if it were all consonant letters
My code is picking up as if it were all consonant letters

Time:04-15

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.")
}

CodePudding user response:

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>

CodePudding user response:

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

CodePudding user response:

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>

  • Related