问题描述
我遇到了一个问题,我试图将参数从一个函数发送到另一个函数,接收参数的函数使用 switch 语句对其进行评估并返回它,但它只返回我放入其中的任何变量例如,而不是高级大师".这是我不明白我做错了什么的代码,请记住我对编码很陌生,也许有人可以给我一些指示.提前致谢.
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();
推荐答案
看看这个表达式就行了
2399 > x > 2200
并取三个值,例如零、2300(这应该返回 true
)和 10000.
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
最终结果总是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.
结论
进行两个与 相关的比较逻辑与 &&
:
Take two comparisons connected with logical AND &&
:
2399 > x && x > 2200
对于一个 switch
语句,您需要在条件部分和其他条件的情况下获取比较值.而直接返回,你可以省略break
,因为函数以它结束.
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();
另一种解决方案可能是使用连续的 if
语句并提前返回.这种方法更好,因为它不会误用 select
的原始思想,即必须比较两个值.
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();
这篇关于JavaScript函数没有将我的参数传递给其他函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!