Javascript Javascript检查字符是否为元音

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26926994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:32:04  来源:igfitidea点击:

Javascript check if character is a vowel

javascriptcharacter

提问by Pizzaman

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.

为什么下面的代码不起作用?该函数检查它是否是元音。然后检查输入以查看其长度是否为 1。如果为 1,则调用该函数。如果大于 1,则请求另一个输入,直到长度为 1。

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

我现在看到 JS 中不存在布尔值。我想这个问题现在无效了!

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

回答by T.J. Crowder

You're calling isVowelin 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 alertas in your last example, or any of several other ways).

isVowel在三个地方调用,只是扔掉了前两个的返回值。如果您想在前两个位置看到返回值,请显示它(alert如上一个示例中所示,或其他几种方式中的任何一种)。

There are other issues as well:

还有其他问题:

  • As devqon points out, you've used booleanrather 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 ifblock (reasonable, consistent formatting would have make that obvious)

  • Your whileloop will never end, because you never update inputwith the return value of the prompt

  • Rather than an iffollowed by a while, use do-while

  • 正如devqon 指出的那样,您使用了boolean而不是var,因此代码不会解析

  • 任何时候你发现自己在写:

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

    ...停下来让它:

    var result = condition;
    return result;
    

    所以对于isVowel

    function isVowel(x) {
    
        var result;
    
        result = x == "A" || x == "E" || x == "I" || x == "O" || x == "U";
        return result;
    }
    

    (当然,您可以将其设为单行,但以这种方式调试更容易。)

  • }在你的if块之后你有一个额外的(合理的、一致的格式会让这很明显)

  • 你的while循环将永远不会结束,因为你永远不更新input用的返回值prompt

  • 而不是if后跟 a while,使用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);

回答by Tang Chanrith

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

回答by JoeOfTex

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

您将字符作为字符串发送给它。它将检测以下任何一项: 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.

幻数只是 AEIOU 的 ASCII 码相乘。检查 c==75 是因为 K 是唯一也得到完美除法的字符,只要我们将检查限制为大写字母。

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.

注意:如果可以删除c = c.charCodeAt(c);代码会运行得更快。它几乎与其他方法一样快,并且此代码仍有可能改进。

回答by Rey Mercado

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

答案可以是一行,简短而甜蜜。见下文。

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

回答by Christina Bright

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

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

return listofVowel.includes(char); }

返回 listofVowel.includes(char); }

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

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

回答by Paras Bisht

(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");
 }   
})(); 

回答by kadhiresan k

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

回答by Len Joseph

Here is a short ES6 helper to test for a vowel

这是一个用于测试元音的简短 ES6 助手

const isLetterAVowel = (str) => {
  return ['a', 'e', 'i', 'o', 'u'].includes(str.charAt(0).toLowerCase());
}

A consonant returns false:

辅音返回false

console.log(isLetterAVowel('cat'));

A vowel returns true:

元音返回true

console.log(isLetterAVowel('emu'));