简体   繁体   English

如何在javavscript中的Switch else语句中写case?

[英]How to write case in Switch else statement in javavscript?

//This is my Problem Complete the getLetter(s) function in the editor. //这是我的问题 在编辑器中完成 getLetter(s) function。 It has one parameter: a string, , consisting of lowercase English alphabetic letters (ie, a through z).它有一个参数:一个字符串,由小写英文字母(即 a 到 z)组成。 It must return A, B, C, or D depending on the following criteria:它必须根据以下条件返回 A、B、C 或 D:

If the first character in string  is in the set {aeiou}, then return A.
If the first character in string  is in the set {bcdfg}, then return B.
If the first character in string  is in the set {hjklm},, then return C.
If the first character in string  is in the set {npqrstvwxyz}, then return D.

I am trying to implement the above scenarion and to dthis I have written the following code. 

//Here is my code
    function getLetter(s) {
        let letter;
        // Write your code here
        
            switch (s) {
            case s.match(/[aeiou]/g).includes(s[0]):
            letter = 'A'
            break; 
            
            case s.match(/[bcdfg]/g).includes(s[0]):
            letter = 'B'
            break; 
        
            case s.match(/[hjklm]/g).includes(s[0]):
            letter = 'C';
            break; 
            
            case s.match(/[npqrstvwxyz]/g).includes(s[0]):
            letter = 'D';
            break; 
            default:
            console.log("hello")
        }
        return letter
    }

The code is showing error. Would any help me to figure it out? How can I implement the above task using switch statement. 

I don't know that much switch case, but maybe you can use parenthesis for the switch statement and if "s.match(/[aeiou]/g).includes(s[0])" return true or false And you forgot ";"我不知道那么多 switch case,但也许你可以在 switch 语句中使用括号,如果“s.match(/[aeiou]/g).includes(s[0])”返回 true 或 false 你忘了“;” after the two first assignment在两次第一次分配之后

the way you wrote the switch case may be partially correct in GoLang.您编写 switch case 的方式在 GoLang 中可能是部分正确的。 but in Javascript, it is not possible to achieve this using Switch-Case statement.但在 Javascript 中,使用 Switch-Case 语句是不可能实现的。

Better try using if-else-if statement something like below最好尝试使用如下所示的 if-else-if 语句

if(s.test(/^[aeiou]/)) {
    return 'A';
} else if(s.test(/^[bcdfg]/)) {
    return 'B'
}

Check out this: Switch statement for string matching in JavaScript .看看这个: JavaScript 中字符串匹配的 Switch 语句 The issue is that match returns null, which breaks the switch statement.问题是 match 返回 null,这会破坏 switch 语句。 Use利用

switch(s){
    case str.match(/^xyz/)?.input:
        //code
        break;

This stops the match from returning null.这会阻止匹配返回 null。

Based on how you're using this, you actually want to switch (true) and not switch (s)根据您的使用方式,您实际上想要switch (true)而不是switch (s)

ie when you have a case y in a switch (x) , the test being done is x === y ;即当你在switch (x)中有一个case y时,正在完成的测试是x === y so you want to do true === some.test()所以你想做true === some.test()

const s = 'a';

switch (true) {
  case /a/.test(s):
    console.log('first match');
    break;
  case /b/.test(s):
    console.log('second match');
    break;
  default:
    console.log('no match');
    break;
}

Will log first match becausefirst match ,因为

  • /a/.test('a') is true /a/.test('a')true
  • and true /* from switch */ === true /* from case test */ and true /* from switch */ === true /* from case test */

Note that in most cases switch can also be written as if..else if..else .请注意,在大多数情况下, switch也可以写成if..else if..else
Base your decision on which to use by which is the most readable (err on the side of if patterns)根据您的决定使用哪个是最易读的(在if模式方面出错)

The above example written as an if might look like this上面写成if的例子可能看起来像这样

const s = 'a';

if (/a/.test(s)) {
  console.log('first match');
} else if (/b/.test(s)) {
  console.log('second match');
} else {
  console.log('no match');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM