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

166
Vistas
Where am I having errors with this Switch control structure in JS?

I am working on a function using Javascript that according a received value (a mark) returns to you the qualification of this mark, according to the Spanish educational system. The code is the following one:

function calculateSpanishClassAverageMark(averageMark) {
    console.log("me muestra mi nota media", averageMark);
    var markText = " ";

    switch (averageMark) {
        case 10:
            markText = "Esta clase ha obtenido una matrícula de honor"
            break;
        case averageMark >= 9 && averageMark < 10:
            markText = "Esta clase ha obtenido un sobresaliente"
            break;
        case averageMark >= 7 && averageMark < 9:
            markText = "Esta clase ha obtenido un notable"
            break;
        case averageMark >= 6 && averageMark < 7:
            markText = "Esta clase ha obtenido un bien"
            break;
        case averageMark >= 5 && averageMark < 6:
            markText = "Esta clase ha obtenido un suficiente"
            break;
        case averageMark >= 4 && averageMark < 5:
            markText = "Esta clase ha obtenido un insuficiente"
            break;
        case averageMark < 4:
            markText = "Esta clase ha obtenido un muy deficiente"
            break;
        default:
            break;
    }

    return console.log("Marktext", markText);
}

The first console.log shows me muestra mi nota media 7.625so the averageMark parameter is being received. However, the console.log that I return shows the following stuff: Marktext . I have tried to debug, and the behavior is that when the program gets to the switch, it goes directly to the default option, so it does not change the value of markText. Where am I making an error? I dont understand why if the averageMark value is 7.625, why it doesnt enter in that case of the switch. Thanks.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The switch statement is for picking out particular values, not ranges.

about 4 years ago · Juan Pablo Isaza Denunciar

0

The switch statement is for picking out particular values, not ranges.

This is true, but there's a trick to use switch as range selector. Obviously if averageMark isn't a number between 0 and 10 the switch will pick the default option or if is higher than 10 will pick the second case.

function FirstCalc(averageMark) {
    
    var markText = " ";
    switch (true) {
        case (averageMark == 10):
            markText = "Esta clase ha obtenido una matrícula de honor"
            break;
        case (averageMark >= 9):
            markText = "Esta clase ha obtenido un sobresaliente"
            break;
        case (averageMark >= 7):
            markText = "Esta clase ha obtenido un notable"
            break;
        case (averageMark >= 6):
            markText = "Esta clase ha obtenido un bien"
            break;
        case (averageMark >= 5):
            markText = "Esta clase ha obtenido un suficiente"
            break;
        case (averageMark >= 4):
            markText = "Esta clase ha obtenido un insuficiente"
            break;
        default:
            markText = "Esta clase ha obtenido un muy deficiente // default option !" 
            break;
    }

    return averageMark + " // " + markText;
}

$('#firstRes').append(FirstCalc(0) + '<br/>');
$('#firstRes').append(FirstCalc(2) + '<br/>');
$('#firstRes').append(FirstCalc(4) + '<br/>');
$('#firstRes').append(FirstCalc(6) + '<br/>');
$('#firstRes').append(FirstCalc(8) + '<br/>');
$('#firstRes').append(FirstCalc(10) + '<br/>');
$('#firstRes').append(FirstCalc(55) + '<br/>');
$('#firstRes').append(FirstCalc(1.25) + '<br/>');
$('#firstRes').append(FirstCalc("word") + '<br/>');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="firstRes"></div>

about 4 years ago · Juan Pablo Isaza 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