Finding the second smallest integer in array

后端 未结 19 1908
無奈伤痛
無奈伤痛 2021-01-18 05:48

We are required in our assignment to find the second smallest integer in one array recursively. However, for the sake of understanding the subject more, I want to do it iter

19条回答
  •  清歌不尽
    2021-01-18 06:37

    I've used Sort function in javascript

    function sumTwoSmallestNumbers(numbers){  
      numbers = numbers.sort(function(a, b){return a - b; });
      return numbers[0] + numbers[1];
    };
    

    by providing a compareFunction for the sort functionality array elements are sorted according to the return value of the function.

提交回复
热议问题