4

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.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
OxfordSi
  • 199
  • 1
  • 3
  • 14

6 Answers6

7

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/

Adam
  • 43,763
  • 16
  • 104
  • 144
0

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

if(letter === vowels[i]) ...
m0sa
  • 10,712
  • 4
  • 44
  • 91
  • Worked a charm - first use of the for in loop so this error will stick in my mind, enough to make me remember your advice. thank you. – OxfordSi Dec 16 '12 at 20:47
0

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.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • 4
    I think that's borderline obfuscated. Why not just say what you mean: `return "aeiou".indexOf(letter) != -1;`? – melpomene Dec 16 '12 at 20:47
  • Because the `~` is shorter, and it is a tiny (insignificant) [bit faster](http://jsperf.com/bitwise-not-versus-1) That said, disliking a specific way to build a `contains` method is no valid reason to downvote a answer. – Cerbrus Dec 16 '12 at 20:50
0

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.

pbhd
  • 4,384
  • 1
  • 20
  • 26
0

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);
    }
}
Shai
  • 3,659
  • 2
  • 13
  • 26
Belle
  • 1
  • 1
0

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

rlarin
  • 147
  • 2
  • 5
  • While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please [include an explanation for your code](//meta.stackexchange.com/q/114762/269535), as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Luca Kiebel Sep 18 '18 at 13:42