Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

339
Views
How to write case in Switch else statement in javavscript?

//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. 
over 4 years ago · Santiago Trujillo
3 answers
Answer question

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

over 4 years ago · Santiago Trujillo Report

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'
}
over 4 years ago · Santiago Trujillo Report

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!