JavaScript函数没有将我的参数传递给其他函数

2023-09-07前端开发问题
2

本文介绍了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.

结论

进行两个与 相关的比较逻辑与 &&:

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

The End

相关推荐

Fatal error: Call to a member function fetch_assoc() on a no
业务场景:使用update语句去更新数据库字段。 原因:update接收值不正确。原来代码: $query = "UPDATE student SET date = now() WHERE id = $id";$result = $mysqli-query($query2) or die($mysqli-error); // 问题出现了在这句 $data = $result-fetch_ass...
2024-12-13 前端开发问题
136

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313

layui树状组件tree怎么默认勾选?
在layui树状组件tree中,勾选问题可以通过以下方法解决: 通过tree的oncheck事件来监听勾选操作,然后根据勾选状态进行相应的处理。例如: tree.on('check', function(obj) { // 获取勾选状态 var isChecked = obj.checked; // 获取当前节点数据 var data =...
2024-11-09 前端开发问题
372

layui laydate日期时间范围,时间默认设定为23:59:59
在Layui中,如果你想设置日期时间选择器(datetime)的默认结束时间为当天的23:59:59,你可以使用如下代码: laydate.render({ elem: '#test10' ,type: 'datetime' ,range: true ,max: '{:date("Y-m-d 23:59:59")}' ,ready: function(date){ $(".layui-laydat...
2024-10-24 前端开发问题
279

layui中表单会自动刷新的问题
layui中表单会自动刷新的问题,因为用到layui的表单,遇到了刷新的问题所以记录一下: script layui.use(['jquery','form','layer'], function(){ var $ = layui.jquery, layer=layui.layer, form = layui.form; form.on('submit(tijiao)', function(data){ a...
2024-10-23 前端开发问题
262