Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

340
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda