Reputation: 33
I'm having a problem where I'm trying to send parameters from one function to another, the function that receives the parameters uses a switch statement to evaluate it and returns it but it just returns what ever variable I put into it instead of "senior Master" for example. Here is the code I don't get what I'm doing wrong, just keep in mind I'm very new to coding maybe someone can give me some pointers. Thanks in advance.
function calculatexxx(x) {
let calculatexxx= x;
switch (x) {
case (x >= 2400):
console.log("Senior Master");
break;
case (2399 > x > 2200):
console.log("National Master");
break;
case (2199 > x> 2000):
console.log("Expert");
break;
case (1999 > x> 1800):
console.log("Class A");
break;
case (1799 > x> 1600):
console.log("Class B");
break;
default:
console.log("Error input not valid");
return(x);
}
}
function displayxxx() {
console.log("Your Rank is: " + calculatexxx(2400)); //
}
displayxxx();
Upvotes: 1
Views: 126
Reputation: 386868
Just have a look to this expression
2399 > x > 2200
and take three values, like zero, 2300 (this should return true
) and 10000.
The expression is executed in order of appearance, because of the same operator.
x 2399 > x value value > 2200 yield result
-------- ------------ ------- ------------ ---------- --------
0 2399 > 0 true true > 2200 1 > 2200 false
2300 2399 > 2300 true true > 2200 1 > 2200 false
100000 2399 > 10000 false false > 2200 0 > 2200 false
The final result is always false
, because the first part returns a boolean value and the converted value is never greater than the last value.
Conclusion
Take two comparisons connected with logical AND &&
:
2399 > x && x > 2200
For taking a switch
statement, you need to take the comparison value in the condition part and in the cases the other condition. And by returning directly, you could omit break
, because the function ends with it.
function calculate(x) {
switch (true) {
case x >= 2400: return "Senior Master";
case x >= 2200: return "National Master";
case x >= 2000: return "Expert";
case x >= 1800: return "Class A";
case x >= 1600: return "Class B";
}
return "Error input not valid";
}
function display() {
console.log("Your Rank is: " + calculate(2400));
}
display();
Another solution could be to use continuing if
statements and return early. This approach is better, because it does not missuse the original idea of select
where two values have to be compared.
function calculate(x) {
if (x >= 2400) return "Senior Master";
if (x >= 2200) return "National Master";
if (x >= 2000) return "Expert";
if (x >= 1800) return "Class A";
if (x >= 1600) return "Class B";
return "Error input not valid";
}
function display() {
console.log("Your Rank is: " + calculate(2400));
}
display();
Upvotes: 8
Reputation: 199
can you try this.
function calculatexxx(x) {
if (x >= 2400){
console.log("Senior Master");
}
else if (2399 > x > 2200){
console.log("National Master");
}
else if (2199 > x> 2000){
console.log("Expert");
}
else if (1999 > x> 1800){
console.log("Class A");
}
else if (1799 > x> 1600):
console.log("Class B");
}
else{
console.log("Error input not valid");
}/////////////
return(x);
}
}
function displayxxx() {
console.log("Your Rank is: " + calculatexxx(2400)); //
}
displayxxx();
Upvotes: 0
Reputation: 23
Firstly you'll have to change your switch conditions from 1799 > x > 1600
to 1799 > x && x >= 1600
Then instead of console.logging in the case itself, you should return "Expert"
etc so the concatination in the function displayxxx actually does something.
Upvotes: 0