【问题标题】:Function gives NAN instead of a string函数给出 NAN 而不是字符串
【发布时间】:2019-11-10 15:19:57
【问题描述】:

我是 JS 的初学者,我对这个函数有疑问: 首先我定义了它:

function highAndLow(numbers){
     numbers = numbers.replace(/ /g,",");
     let high = Math.max(numbers);
     let min = Math.min(numbers);
     console.log("The max value is "+high+ ", and the minimum is "+min);
};

当我调用它时:

console.log(highAndLow("1 2 3 4 5"));

我得到以下输出:

The max value is NaN, and the minimum is NaN

所以请您指出问题出在哪里,并提前致谢。

【问题讨论】:

    标签: javascript function output


    【解决方案1】:
    function highAndLow(numbers) {
         numbers = numbers.split(' ').map(n => +n);
         let high = Math.max(...numbers);
         let min = Math.min(...numbers);
         return "The max value is "+ high+ ", and the minimum is "+ min;
    }
    
    console.log(highAndLow("1 2 3 4 5"));
    

    【讨论】:

      【解决方案2】:

      你犯了三个错误。

      • 您在字符串上使用replace()。这将输出一个字符串。您不能将字符串传递给Math.min/max。你应该使用split() by ' '
      • 即使您split(),数组的元素也将是字符串。您可以使用map() 将其转换为数字
      • Math.min/max 不期望数组作为参数。如果你想传递数组使用Spread Operator

      function highAndLow(numbers){
           numbers = numbers.split(' ').map(Number)
           let high = Math.max(...numbers);
           let min = Math.min(...numbers);
           console.log("The max value is "+high+ ", and the minimum is "+min);
      };
      
      console.log(highAndLow("1 2 3 4 5"));

      【讨论】:

        【解决方案3】:

        试试这个

        function highAndLow(numbers){
             numbers = numbers.split(" ");
             let high = Math.max(...numbers);
             let min = Math.min(...numbers);
             console.log("The max value is "+high+ ", and the minimum is "+min);
        };
        
        
         highAndLow("1 2 3 4 5");

        【讨论】:

        • 非常感谢,我有一个问题请教:为什么在第三行和第四行的变量号前加上这三个点?
        • @MohammedTouati 那就是所谓的传播算子。 Math.max/min 需要像 Math.max(1,2,3) 这样的单个参数。所以要将Math.max([1,2,3]) 转换为Math.max(1,2,3) 使用扩展运算符。
        • @MohammedTouati ,这是传播(ES5),将 [1,2,3,4,5]} 更改为 1,2,3,4,5 。因为 min 和 max 函数只接受元素
        【解决方案4】:

        我认为主要的误解是:

         Math.max("1,2,3")
        

          Math.max(1, 2, 3)
        

        是平等的(第一个是你做的,第二个是你想要的)。它们不像在第一种情况下您将字符串传递给Math.max,而在第二种情况下您传递多个数字。要将字符串转为数字数组,可以先.split转为字符串数组,再将字符串解析为数字。然后可以将该数组作为参数传播:

          Math.max(..."1 2 3".split(" ").map(Number))
        

        【讨论】:

          【解决方案5】:

          问题是 Math.max 需要一个集合,而您正在传递一个仅包含数字的字符串。 CO将集合转换为数组并传递它。

          function highAndLow(numbers){
               numbers = numbers.replace(/ /g,",");
               var numbersList = numbers.split(",").map(function(num){
                return parseInt(num);
               });
               let high = Math.max(...numbersList);
               let min = Math.min(...numbersList);
               console.log("The max value is "+high+ ", and the minimum is "+min);
          };
          
          console.log(highAndLow("1 2 3 4 5"));

          【讨论】:

            猜你喜欢
            • 2014-04-09
            • 1970-01-01
            • 1970-01-01
            • 2021-06-21
            • 1970-01-01
            • 1970-01-01
            • 2020-07-22
            • 2014-12-14
            • 2018-03-25
            相关资源
            最近更新 更多