'How to assign a new value in switch case which in a function?
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);
Solution 1:[1]
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;
//...
}
Solution 2:[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);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Roakz |
| Solution 2 | NathanWindisch |
