Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

342
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda