简体   繁体   English

我需要使用 switch 语句从单词集中返回一个单词

[英]I need to use switch statment to return a word from the set of words

Complete the getLetter(s) function in the editor.在编辑器中完成 getLetter(s) 函数。 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, then return A. If the first character in string is in the set, then return B. If the first character in string is in the set, then return C. If the first character in string is in the set, then return D. Hint: You can get the letter at some index in using the syntax s[i] or s.charAt(i).如果string的第一个字符在集合中,则返回A。如果string的第一个字符在集合中,则返回B。如果string的第一个字符在集合中,则返回C。如果string的第一个字符在集合中字符串在集合中,然后返回 D。提示:您可以使用语法 s[i] 或 s.charAt(i) 获取某个索引处的字母。

function getLetter(s) {
    let letter;
    //letter=s.charAt(0)
    const set1= new Set(['a','e','i','o','u']);
    const set2=new Set(['b','c','d','f','g']);
    const set3=new Set(['h','j','k','l','m']);
    const set4=new Set(['n','p','q','r','s','t','v','w','x','y','z']);
    // Write your code here
    switch(s.charAt(0)){
        
        case 1:
        if(set1.has(s.charAt(0)))
        letter=A;
        break;
        
        case 2:
        if(set2.has(s.charAt(0)))
        letter=B;
        break;
        
        case 3:
        if(set3.has(s.charAt(0)))
        letter=C;
        break;
        
        case 4:
        if(set4.has(s.charAt(0)))
        letter=D;
        break;
    }
    return letter;
}

Wanted to know what is wrong in this code, I am getting undefined in the result.想知道这段代码有什么问题,结果是 undefined 。

You could check against the set with a boolean value.您可以使用布尔值检查集合。

 function getLetter(s) { let letter; const set1 = new Set(['a', 'e', 'i', 'o', 'u']); const set2 = new Set(['b', 'c', 'd', 'f', 'g']); const set3 = new Set(['h', 'j', 'k', 'l', 'm']); const set4 = new Set(['n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z']); // Write your code here switch (true) { case set1.has(s[0]): letter = 'A'; break; case set2.has(s[0]): letter = 'B'; break; case set3.has(s[0]): letter = 'C'; break; case set4.has(s[0]): letter = 'D'; break; } return letter; } console.log(getLetter('e')); console.log(getLetter('b')); console.log(getLetter('k')); console.log(getLetter('s'));

You want a function to check the string.你想要一个函数来检查字符串。 but in all switch cases, you are trying to check what?但是在所有开关情况下,您都在尝试检查什么? switch(s.charAt(0)) this will be evaluated, s.charAt will never can be a number without casting. switch(s.charAt(0))这将被评估,s.charAt 将永远不会是一个没有转换的数字。 So suggestions make always a default case (which will work always) and case所以建议总是一个默认案例(这将永远有效)和案例

switch (true) {
case set1.has(s.charAt(0)):
  return 'A'

case set2.has(s.charAt(0)):
  return 'B'

case set3.has(s.charAt(0)):
  return 'C'

case set4.has(s.charAt(0)):
  return 'D'
default:
  return 0

} }

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

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