OxfordSi
OxfordSi

Reputation: 177

"Check if a letter is a vowel" - Function returning false?

Exercise: Write a function that takes a character (i.e. a string of length 1) and returns true if it is a vowel, false otherwise.

My Code:

var findVowel = function(letter) {

var vowels = ["a", "e", "i", "o", "u"];

for(var i in vowels){

    if(letter === i){
        return true;
    } else {
        return false;
    }
}

};

findVowel("e");

I've researched high and low and to me the code looks as if it should but it returns false despite if a vowel is given or not.

Upvotes: 4

Views: 20994

Answers (6)

rlarin
rlarin

Reputation: 147

try

function checkIfVowel(vowel) {  
  return /[aeiouAEIOU]/.test(vowel); 
}

this function take a character parameter and checks if it is a vowel through regex, finally returns the corresponding value (true or false).

Upvotes: 0

Belle
Belle

Reputation: 1

You could also use the .charAt method with the .toLowerCase method.

function vowelChecker(x) {
    // vowelChecker will grab the first letter (character)...
    var firstChar = x.toLowerCase().charAt(0);

    // Then check if that first letter is a vowel.
    if (firstChar === "a" || firstChar === "e" || firstChar === "i" || firstChar === "o" || firstChar === "u") {
        // If so... it will log true.
        console.log(true);
    } else {
        // If not... it will log false.
        console.log(false);
    }
}

Upvotes: 0

Cerbrus
Cerbrus

Reputation: 72857

Simply use this, no need to loop through anything:

var findVowel = function(letter) {
    return "aeiou".indexOf(letter) != -1; // return if the letter is found in "aeiou"
};

Or, my personal favorite:

var findVowel = function(letter) {
    return ~"aeiou".indexOf(letter);
};

.indexOf() returns -1 if the parameter isn't found in the string, otherwise, it returns the parameter's position in the string (a int from 0 to string length - 1).

So in the first sample, if the .indexOf() returns -1, the letter is not a vowel. If it returns any other value, it is. (Hence the != -1).

The ~ is a bitwise NOT, inverting that output:
-1 becomes 0 --> a false-ish value.
X (Where X is positive or 0) becomes -(X+1) --> A true-ish value.

This way, the function will return true-ish if the letter is a vowel, and false-ish if it's not.

If you need a "strict" boolean output, replace the return with this:

return !!~"aeiou".indexOf(letter);

The !! is a double boolean NOT (So, invert the boolean value twice), this casts the True-ish value to a true, and a false-ish value (0) to a false.

Upvotes: 0

Adam
Adam

Reputation: 44929

Don't use for..in loops with arrays. i is the index not the value. Also, your code will only check the letter "a". It will never go to the next iteration of the loop because it always returns true or false after the first iteration.

You need to move return false to be after the loop, so that it will only return false after it has checked against all vowels.

You should also switch to the more "traditional" for..loop style.

I won't even get into the whole "is 'y' a vowel?" issue" :)

Here's the fixed up code:

var findVowel = function(letter) {

    var vowels = ["a", "e", "i", "o", "u"];

    for(var i = 0; i < vowels.length; i++){ // don't use for...in with Arrays
        if(letter === vowels[i]){// Use array indexing instead
            return true;
         }
    }

    return false;// This is after the loop

};

Try it out on: http://jsfiddle.net/adamzr/3yhFS/

Upvotes: 7

pbhd
pbhd

Reputation: 4467

You compare only the first element in your vowels to the given letter and return that result. Instead, you have to go thru your whole array, to see if any of your vowels matches.

Upvotes: 0

m0sa
m0sa

Reputation: 10940

i is the current index in the iterator of vowels, not the current vowel, hence:

if(letter === vowels[i]) ...

Upvotes: 0

Related Questions