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

165
Views
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 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

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