Define whether the letter is vowel or consonant

Asked

Viewed 1,299 times

12

I was trying to create a code with Javascript that after reading the letter entered showed if it was a vowel or consonant but with any letter I put the code answers me that and a consonant even if it is a vowel.

I can’t find the error in the code.

var valorlido
function lerletra() {
    valorlido = prompt("Introduza uma letra")
    return valorlido
}

function isvogal(caracter) {
    switch (caracter) {
        case "a":
        case "A":
        case "e":
        case "E":
        case "i":
        case "I":  
        case "o":
        case "O":
        case "u":
        case "U": 
        break
        return true
        default:
        return false
    }
}

var letra
lerletra ();
if (isvogal(letra) == true) {
    alert("A letra é Vogal")
} else {
    alert("A letra é consoante")
}

  • 2

    Take the break is leaving the switch without returning

  • 2

    puts break after Return

3 answers

13

You can also solve your problem by creating an array of vowels and checking whether the value typed in prompt is inside the array, using the function indexOf, thus:

let letra = prompt("Insira uma letra")

let vogais = ["a", "e", "i", "o", "u"]

if (vogais.indexOf(letra.toLowerCase()) !== -1) {
  alert("Vogal")
} else {
  alert("Consoante")
}

  • 1

    Excellent answer Gabriel, I noticed in your answers the same thing, if the code is reproducible, make an executable answer, thus increases the readability and knowledge of your answer.

  • 1

    Thank you for the suggestion, I will pay attention to it in the next few times.

  • I already had an answer with indexOf. I’m gonna erase mine.

13

You can also use the test() method, which executes a search for a match between a regular expression and a string and returns true or false.

function isVogal(char) {
    return /^[aeiou]$/.test(char.toLowerCase());
}
alert(isVogal(prompt("Introduza uma letra")) ? "Vogal" : "Consoante");

And as Luiz Felipe commented below: If you use the flag i in regular expression will eliminate the need for toLowerCase.

function isVogal(char) {
    return /^[aeiou]$/i.test(char);
}
alert(isVogal(prompt("Introduza uma letra")) ? "Vogal" : "Consoante");

  • 3

    If you use the flag i in regular expression will eliminate the need for toLowerCase. :)

9


The code has several errors. One of them is that it gives a break before returning something, then ends up returning to another, the break has to be the last instruction of the block, but if you’re going to give a return you don’t even have to wear a break because it will never be achieved.

You are not holding the value read in any variable. I kept the variable`letter, but I didn’t need it at all.

I’ve made some other changes that may not affect the outcome but that if you get used to doing so you may have problems in the future, so pay attention to all the details that I’ve changed.

function lerLetra() {
    return prompt("Introduza uma letra");
}

function isVogal(caracter) {
    switch (caracter) {
        case "a":
        case "A":
        case "e":
        case "E":
        case "i":
        case "I":  
        case "o":
        case "O":
        case "u":
        case "U": 
            return true;
        default:
            return false;
    }
}

var letra = lerLetra();
if (isVogal(letra)) alert("A letra é vogal");
else alert("A letra é consoante");

I usually respond to beginners within the same algorithm that the person is doing to solve that point problem, because without explanation of each new concept the person who is starting will not understand anything that was put there. If someone else’s wish is to achieve the same result more simply and probably efficiently you can do it in a row, because if it’s to be simple then make it simple once and for all:

alert("aeiouAEIOU".indexOf(prompt("Insira uma letra")) !== -1 ? "A letra é vogal" : "A letra é consoante");

I put in the Github for future reference.

  • worked! thanks within 8 min I mark as correct the answer!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.