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

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

0

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

about 4 years ago · Juan Pablo Isaza Relatório

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 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