Ad

When you sort a list in descending order the min are in the first position and the max in the last position.

Code
Diff
  • var getMin = function (list){
      //Clean the original list, discard numbers lower than 1 and discard nulls, objects, strings...
      var filteredList = list.filter((n) => n > 0);
      //Sort the sanitize list in descending order.
      filteredList.sort((a,b) => a > b);
      //If filteredList are 0length there aren't positive integers. In other case the result is at first element in the lisst.  
      return filteredList.length == 0?0:filteredList[0];
    }
    • var getMin = function (list){
    • var min = Number.MAX_VALUE;
    • for (var i = 0; i < list.length; i++) {
    • if (+list[i] <= 0) {
    • continue;
    • }
    • min = Math.min(min, +list[i]);
    • }
    • return min = min === Number.MAX_VALUE ? 0 : min;
    • //Clean the original list, discard numbers lower than 1 and discard nulls, objects, strings...
    • var filteredList = list.filter((n) => n > 0);
    • //Sort the sanitize list in descending order.
    • filteredList.sort((a,b) => a > b);
    • //If filteredList are 0length there aren't positive integers. In other case the result is at first element in the lisst.
    • return filteredList.length == 0?0:filteredList[0];
    • }