JavaScript函数未将我的参数传递给其他函数 [英] JavaScript function not passing my parameters to other function

查看:37
本文介绍了JavaScript函数未将我的参数传递给其他函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试将参数从一个函数发送到另一个函数时遇到问题,接收参数的函数使用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.

结论

进行两个与逻辑AND && :

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函数未将我的参数传递给其他函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆