Return highest and lowest number in a string of numbers with spaces

后端 未结 4 1897
伪装坚强ぢ
伪装坚强ぢ 2021-01-03 14:14

Let\'s say I have a string of numbers separated by spaces and I want to return the highest and lowest number. How could that best be done in JS using a function? Example:

4条回答
  •  悲&欢浪女
    2021-01-03 14:28

    function highAndLow(numbers){
      var temp = numbers.split(' ');
      temp.sort(function(a,b){return a-b; });
      return  temp[temp.length-1] + ' ' + temp[0];
    }
    

    did a little differently: first split into an array, then sorted ... and returned the last (maximum) element with the first (minimum) element

提交回复
热议问题