Javascript check if character is a vowel

青春壹個敷衍的年華 提交于 2019-12-11 02:20:42

问题


I've looked at plenty of questions related to mine here but they're all using different methods to the method I have to use. I'm aware it's a very long winded way to find out when there are simpler ways but I'm just following instructions.

Why doesn't the below code work? The function checks if it's vowel. Then the input is checked to see if it's length is 1. If it's 1, call the function. If it's greater than 1, ask for another input until the length is 1.

I see now that boolean doesn't exist in JS. I guess this question is invalid now!

function isVowel(x){

    boolean result;

        if(x == "A" || x == "E" || x == "I" || x == "O" || x == "U" ) {
            result = true;
        }
        else{
            result = false;
        }
    return result;
    }

    var input;


    input = prompt("Enter a character ");
    input = input.toUpperCase();
    if(input.length == 1){
        isVowel(input);
        }
    }
    else{
        while(input.length != 1){
            prompt("Enter a character ");
            if(input.length == 1){
                isVowel(input);
            }
        }
    }

    alert(isVowel(input));

回答1:


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



回答2:


Try This function no matter what uppercase or lowercase

function isVowel(x) {  return /[aeiouAEIOU]/.test(x); }

var input = '';
while (input.length != 1) {
  input = prompt("Enter a character ");
}
alert(isVowel(input));



回答3:


I wanted a mathematical solution, so I came up with this:

function isVowel(c) {
    c = c.charCodeAt(c);
    var magicNumber = 2198524575;
    c = (c > 96) ? (c-32) : c;
    if( c < 65 || c == 75 || c > 90) 
        return false;
    var div = magicNumber / c;
    var diff = div - Math.floor(div);
    if( diff == 0 )
        return true;
    return false;
}

You send it the character as a string. It will detect any of these: AEIOUaeiou

The magic number is just the ascii codes for AEIOU multiplied together. The check c==75 is because K is the only character that also gets a perfect division, as long as we constrain the checks to the upper case letters.

Note: If you can remove c = c.charCodeAt(c); the code would run faster. It's almost as fast as other methods, and its possible this code could still be improved.




回答4:


The answer can be one line and is short and sweet. See below.

    function isVowel(x) { 
        return ("aeiouAEIOU".indexOf(x) != -1); 
    }



回答5:


(function(){
     var vowel_string = "aieouAIEOU";
     input = prompt("Enter a single character...");
     if (input.length == 1){
       if (vowel_string.contains(input)){
            alert(input + " is a vowel");
  }
 }
 else{
     alert("Enter a single character");
 }   
})(); 



回答6:


To check if the given string contains a vowel or not in a simple way

var input = "kad aeiou";
function vowvelOrNot(input){
  var str = input.toLowerCase();
  if(str.length > 0){
    for(i=0; i <= str.length; i++){
      switch (str[i]){
        case 'a':
          var flag_a = true;
          break;
        case 'e':
          var flag_e = true;
          break;
        case 'i':
          var flag_i = true;
          break;
        case 'o':
          var flag_o = true;
          break;
        case 'u':
          var flag_u = true;
          break;
      }
      if(flag_a && flag_e && flag_i && flag_o && flag_u){
        return "Given string is Vowel";
      }
    }
    return "Given string is Not Vowel";
  }else{
    return "please enter the correct value";
  }
}
var res = vowvelOrNot(input);
console.log(res);



回答7:


function is_vowel(char){ let listofVowel= ['a','i','e','o','u','A','I','E','O','U'];

return listofVowel.includes(char); }

console.log(is_vowel('a'));



来源:https://stackoverflow.com/questions/26926994/javascript-check-if-character-is-a-vowel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!