最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Setting up a if else function that detects if it is a vowel - Stack Overflow

programmeradmin3浏览0评论

I am new to JavaScript and I know I did something wrong because I keep getting true as a result. Can someone please point out my mistake? Thanks in advance.

function checkForVowel(x){
   if(x == "a", "e", "i", "o", "u"){
       document.write("true");
   }else{
       document.write("false");
   }
}

checkForVowel("n");

I am new to JavaScript and I know I did something wrong because I keep getting true as a result. Can someone please point out my mistake? Thanks in advance.

function checkForVowel(x){
   if(x == "a", "e", "i", "o", "u"){
       document.write("true");
   }else{
       document.write("false");
   }
}

checkForVowel("n");
Share Improve this question edited Jun 7, 2012 at 2:06 Derek 朕會功夫 94.5k45 gold badges198 silver badges253 bronze badges asked Jun 7, 2012 at 1:24 user1324518user1324518 111 silver badge4 bronze badges 1
  • 1 Take a look here stackoverflow./questions/5488028/… there are many ways to check for a vowels – G-Man Commented Jun 7, 2012 at 1:27
Add a ment  | 

3 Answers 3

Reset to default 11

JavaScript's parison operator isn't magic; you'll need to do each parison separately. You can check for "any of these" using a logical OR, ||:

if(x == "a" || x == "e" || x == "i" || x == "o" || x == "u")

Or, since you have many single-character strings, you can make use of indexOf. But the above might be easier to understand if you're just starting out.

if(x.length === 1 && 'aeiou'.indexOf(x) > -1)

As for the reason you keep getting true, it's that , is actually an operator in JavaScript, mainly invented to confuse people1. It evaluates its left side, then evaluates its right side, and returns the right side. So your expression boils down like this:

if(x == "a", "e", "i", "o", "u")
if("e", "i", "o", "u")
if("i", "o", "u")
if("o", "u")
if("u")

Which is a truthy value.

1 Not actually.

Sometimes the code is a lot cleaner to test against a data structure than use lots of if/else code. Which is faster may depend upon the specific browser engine and how many parisons you have. This one looks to see if the string is present in a string of vowels:

var vowels = "aeiou";

function checkForVowel(x) {
    return(vowels.indexOf(x.toLowerCase()) >= 0);
}

This one tests to see if the test char is in a javascript object which isn't needed here, but is another interesting way to test if something is in a set:

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

function checkForVowel(x) {
    return(x.toLowerCase() in vowels);
}

I don't know why, but no one suggested this. :/

function checkForVowel(x){
    document.write(/[aeiou]/i.test(x));   //Clearly this is even simpler than
}                                         // other methods.

Or

function checkForVowel(x){
   if(x.search(/[aeiou]/i) != -1){
       document.write("true");
   }else{
       document.write("false");
   }
}
发布评论

评论列表(0)

  1. 暂无评论