【问题标题】:Ternary function return Nan in Node jsNode js中的三元函数返回Nan
【发布时间】:2021-10-20 07:55:12
【问题描述】:

代码:

function sumfunct(x,y)
{
    let sum = 0;
    sum = (x === null ? 0 : x) + (y === null ? 0 : y);
    return sum;
}

如果其中一个输入值为 null,则返回 Nan

请帮我返回一些值

【问题讨论】:

  • 如果调用函数时不带一个或多个参数,则函数中的值为undefined,而不是null
  • 1) undefined === null => false; 2)1 + undefined => NaN

标签: javascript node.js conditional-operator


【解决方案1】:

在 JavaScript 中所有这些值:(0, undefined, null, false, '') 将返回 false。

如果你使用三重 = 就像 ===。这也检查 TYPE。所以如果你有 undefined 作为一个值,这不会是 === 为 null。

在这里检查简单的功能,这可能会解决您的问题。

function sumfunct(x,y)
{
    let sum = 0;
    sum = (x ? x : 0) + (y ? y : 0);
    return sum;
}

【讨论】:

    【解决方案2】:

    逻辑运算符=== 匹配类型和值。如果您不传递参数,则默认值为undefined,它不为空,因此您将一个数字添加到 undefined。

    function sumfunct(x,y)
    {
        let sum = 0;
        sum = (!x ? 0 : x) + (!y ? 0 : y);
        return sum;
    }
    

    【讨论】:

      【解决方案3】:

      为您的函数参数添加默认值。

      const sum = (x = 0, y = 0) => x + y;
      

      如果它们是nullundefined,则值为0;

      为了更好的处理,你可以使用类似的东西:

      const sum = (x, y) => {
        const a = Number.isFinite(+x) ? +x : 0;
        const b = Number.isFinite(+y) ? +y : 0;
        return a + b;
      }
      

      【讨论】:

        【解决方案4】:

        您可以将默认值作为0.undefinenull 替换为0

        function sumfunct(x=0,y=0)
        {
            let sum = 0;
            sum = x + y;
            return sum;
        }
        
        sumfunct(console.log(sumfunct(2, undefined)))
        sumfunct(console.log(sumfunct(2, null)))

        【讨论】:

          【解决方案5】:

          您也可以分别检查两个输入

          function sumfunct(x,y)
          {
              x = (x === null ? 0 : x);
              y = (y === null ? 0 : y);
              return x+y;
          }
          
          console.log(sumfunct(2,2));
          console.log(sumfunct(2,null));

          【讨论】:

            【解决方案6】:

            一种更短的方法。每个 falsey 值(0nullundefined)都将转换为 0:

            function sumfunct(x,y)
            {
                let sum = 0;
                sum = (x || 0) + (y || 0);
                return sum;
            }
            

            在这里试试 sn-p

            function sumfunct(x,y)
            {
                let sum = 0;
                sum = (x || 0) + (y || 0);
                return sum;
            }
            
            console.log(sumfunct(2, 2));   // 4
            console.log(sumfunct(3));     // 3
            console.log(sumfunct(null, 2));   // 2
            console.log(sumfunct(undefined, undefined));   // 0

            【讨论】:

            • ...此时,sum 无关紧要,可以写成const sumFunct = (x, y) => (x || 0) + (y || 0)
            【解决方案7】:

            您正在尝试将两个不是数字的变量求和,y 或 x,也许其中一个是未定义的。尝试控制台记录 x 和 y。

            【讨论】:

              猜你喜欢
              • 2021-08-06
              • 1970-01-01
              • 2020-06-01
              • 1970-01-01
              • 2012-09-08
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多