-1

//This is my Problem Complete the getLetter(s) function in the editor. It has one parameter: a string, , consisting of lowercase English alphabetic letters (i.e., a through z). It must return A, B, C, or D depending on the following criteria:

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. 

4 Answers4

0

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 ";" after the two first assignment

Clément
  • 3
  • 2
0

the way you wrote the switch case may be partially correct in GoLang. but in Javascript, it is not possible to achieve this using Switch-Case statement.

Better try using if-else-if statement something like below

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

Check out this: Switch statement for string matching in JavaScript. The issue is that match returns null, which breaks the switch statement. Use

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

This stops the match from returning null.

0

Based on how you're using this, you actually want to switch (true) and not switch (s)

i.e. when you have a case y in a switch (x), the test being done is x === y; so you want to do 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 because

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

Note that in most cases switch can also be written as if..else if..else.
Base your decision on which to use by which is the most readable (err on the side of if patterns)

The above example written as an if might look like this

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');
}
Paul S.
  • 64,864
  • 9
  • 122
  • 138