Javascript Check If Character Is a Vowel

Javascript check if character is a vowel

You're calling isVowel in three places, and just throwing away the return value in the first two. If you want to see the return value in the first two places, show it (via alert as in your last example, or any of several other ways).

There are other issues as well:

  • As devqon points out, you've used boolean rather than var, so the code won't parse

  • Any time you find yourself writing:

    var result;
    if (condition) {
    result = true;
    } else {
    result = false;
    }
    return result;

    ...stop and make it:

    var result = condition;
    return result;

    So for isVowel:

    function isVowel(x) {

    var result;

    result = x == "A" || x == "E" || x == "I" || x == "O" || x == "U";
    return result;
    }

    (You can, of course, make that a one-liner, but it's easier to debug this way.)

  • You have an extra } after your if block (reasonable, consistent formatting would have make that obvious)

  • Your while loop will never end, because you never update input with the return value of the prompt

  • Rather than an if followed by a while, use do-while

Here's an updated version with just those changes

function isVowel(x) {
var result;
result = x == "A" || x == "E" || x == "I" || x == "O" || x == "U"; return result;}
var input;
do { input = prompt("Enter a character "); if (input.length == 1) { alert(isVowel(input)); }} while (input.length != 1);

Checking a character is a vowel

Because once you've found a match, you need to break from the loop and exit the function.

Correct way to handle false:

function isItAVowel(letter) {
var vowel = ["a", "e", "i", "o", "u"];
for (var i=0; i < vowel.length; i++) {
if (letter == vowel[i]) {
document.getElementById("paragraph").innerHTML = "vowel";
return;
}
}
document.getElementById("paragraph").innerHTML = "not vowel";
}

isItAVowel("i");

Since everyone is posting fun ways of doing this differently, here is a way to do it without a function at all, with the fastest execution:

var isItAVowel = {
'a': true,
'e': true,
'i': true,
'o': true,
'u': true };

!!isItAVowel["a"] == true;
!!isItAVowel["o"] == true;
!!isItAVowel["c"] == false;

Check if is vowel or consonant

To tweak your current code, you can have a variable which indicates whether the character is a consonant, and starts out as true, and if you find a vowel, reassign it to false, then check that variable:

function letter(arg) {  var vowel = "aeouiAEOUI";  var result = "";  let isConsonant = true;  for (var i = 0; i < vowel.length; i++) {    if (arg === vowel[i]) {      isConsonant = false;    }  }  if (isConsonant) {    console.log("consonant");  } else {    console.log("vowel");  }}
letter('a');letter('f');

How to find if the first letter of a string is a vowel in javascript

You're quite close, just slice the word using [0] and check that way:

function startsWithVowel(word){   var vowels = ("aeiouAEIOU");    return vowels.indexOf(word[0]) !== -1;}
console.log("apple ".concat(startsWithVowel("apple") ? "starts with a vowel" : "does not start with a vowel"));console.log("banana ".concat(startsWithVowel("banana") ? "starts with a vowel" : "does not start with a vowel"));

How to check if a string character is in a JavaScript array?

Is it this what you're looking for?

function checkVowel(str) {
const counts = Object.seal({ a: 0, e: 0, i: 0, o: 0, u: 0 });

for (let char of str) {
counts[char.toLowerCase()]++;
}

return counts;
}

console.log(checkVowel("hello world"));

JavaScript: vowell or consonant function

Open the dev tools and see what the console tells you.

I see three mistakes :

  • you're missing a closing } bracket at the end of the function.
  • you're using the reserved word let as a variable name. Say you use letter instead. JS engines may find their way out of your let, but it's bad practice anyway. Besides, since you renamed let to letter, you may want to rename your function to something that actually means what it does, such as displayPromptLetterType.
  • The way to retrieve the user's input from the field near the button is var letter = document.getElementById("prompt").value;

Besides, rather than a long if condition, you may want it a tad more data driven :

    var vowels = ["a","e","i","o","u"];
if (vowels.indexOf(letter.toLowerCase()) >= 0) {
...


Related Topics



Leave a reply



Submit