How to assign a new value in switch case which in a function?
Asked Answered
L

4

5

Currently writing a function that converts a numerical grade into American system. Basically the result should be like this:

You got a D (60%)!

But I get this :

You got a 60 60%!

Apart from the brackets what should I do to make it look like as much as possible?

the code is below:

function gradeConverting(grade) {
    let gradePercent = grade + "%";
    switch (grade) {
        case (90 < grade && grade <= 100):
            grade = "A";
            break;
        case (80 < grade && grade <= 89):
            grade = "B";
            break;
        case (70 < grade && grade <= 79):
            grade = "C";
            break;
        case (60 <= grade && grade <= 69):
            grade = "D";
            break;
        case (50 <= grade && grade <= 59):
            grade = "E";
            break;
        case (grade <= 49):
            grade = "F";
            break;
    }
    return console.log("You got a " + grade + " " + gradePercent + "!");
}

gradeConverting(55);
Lancaster answered 29/1, 2020 at 17:21 Comment(0)
O
9

The logic of your code is completely valid but you are using switch() in a wrong way.

check the doc about : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch

if you put switch(grade) then something in case something is expect a value that match grade instead of an expression that return true|false

For example:

switch (grade) {
        case 90: // only for grade === 90
            grade = "A";
            break;
        case 55: // only for grade === 55
        ...

Indeed you can have a function with multiple ifstatement then return lettergrade. Or still use the current logic with some modification and still utilize switch().

I create another variable for lettergrade, recommend don't modify grade unless you know what you are trying to do.

function gradeConverting(grade) {
    let gradePercent = grade + "%";
    var lettergrade = ""; 
    switch (true) { // change to true 
        case (90 <= grade && grade <= 100):
        // you want (90 <= grade && grade <= 100) to be evaluated as true in order to execuate
            lettergrade = "A";
            break;
        case (80 <= grade && grade <= 89):
            lettergrade = "B";
            break;
        case (70 <= grade && grade <= 79):
            lettergrade = "C";
            break;
        case (60 <= grade && grade <= 69):
            lettergrade = "D";
            break;
        case (50 <= grade && grade <= 59):
            lettergrade = "E";
            break;
        case (grade <= 49):
            lettergrade = "F";
            break;
    }
    return console.log("You got a " + lettergrade + " (" + gradePercent + ")!");
}

gradeConverting(100);
Oxus answered 29/1, 2020 at 17:55 Comment(0)
N
2

You are not using using correctly the switch statement.

It is typically used with fixed values, like

switch (value) {
    case 1:
        // do something for value == 1
        break;
    case 2:
        // do something for value == 1
        break;
    //...
}

Additionally, you have no conversion for some values (90, 80 and 70) since you have strange bounds checks using inconsistently < and <=. (Change > to >= in the below code if you need to the lower tens included)

In your case, a sequence of if/else seems to be more appropriate:

function gradeConverting(grade) {
    let gradePercent = grade + "%";
    let americanGrade
    // note: you should probably specify what happens if grade > 100
    if (grade > 90) {
        americanGrade = "A";
    } else if (grade > 80) {
        americanGrade = "B";
    } else if (grade > 70) {
        americanGrade = "C";
    } else if (grade > 60) {
        americanGrade = "D";
    } else if (grade > 50) {
        americanGrade = "E";
    } else {
        americanGrade = "F";
    }
    return console.log("You got a " + americanGrade + " " + gradePercent + "!");
}

gradeConverting(55);

Note that you could shorten the code, for instance like this:

function toAmericanGrade(grade) {
    if (grade < 50) {
        return "F";
    } else if (grade >= 100) {
        return "A";
    }
    // `"F".charCodeAt(0) - 1 + 5` = `74`
    return String.fromCharCode(74 - Math.floor((grade)/10))
}

function gradeConverting(grade) {
    let gradePercent = `${grade}%`;
    let americanGrade = toAmericanGrade(grade)
    return console.log(`You got a ${americanGrade}${gradePercent}!`);
}

gradeConverting(55);

// test
for(let i = 45; i <= 100; i++) {
    gradeConverting(i);
}
Nielsen answered 29/1, 2020 at 23:11 Comment(1)
thanks for the input. But you may understand that I am just a newbie in this :) so your solution is a bit complex for now. But thanks again for the explanation, I got what you mean.Lancaster
R
2

Incase of assigning a switch statement to a variable. The idea is to use a Instant-Invoke-Function, with or without argument.

const today = (function(day){
     switch(day){
       case 1: return "Monday";
       case 2: return "Tuesday";
       case 3: return "Wednesday";
       case 4: return "Thursday";
       case 5: return "Friday";
       case 6: return "Saturday";
       case 7: return "Sunday";
       // to prevent undefined from return, add default case.
       default: return "Monday";
     }})(2);
Runaway answered 4/5 at 10:55 Comment(0)
I
0

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch

The MDN docs are one of the best resources for these types of Javascript syntax issues!

But essentially a switch statement in Javascript will only accept 1 expression followed but multiple outcomes of that expression.

Which within each specific case you can act on that outcome.

switch (value) {
    case 1:
        // do something for value == 1
        break;
    case 2:
        // do something for value == 1
        break;
    //...
}
Ignatia answered 30/1, 2020 at 0:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.