When you sort a list in descending order the min are in the first position and the max in the last position.
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];
- }
describe("Solution", function(){ it("test1", function(){ var list = [0, 1, 1, 3]; Test.assertEquals(getMin(list), 1, "test fails"); }); it("test2", function(){ var list = [7, 2, null, 3, 0, 10]; Test.assertEquals(getMin(list), 2, "test fails"); }); it("test3", function(){ var list = [null, -2]; Test.assertEquals(getMin(list), 0, "test fails"); }); it("test4", function(){ var list = [5,null, -2, {a:"5"}, [1,2,3],3]; Test.assertEquals(getMin(list), 3, "test fails"); }); });
- describe("Solution", function(){
- it("test1", function(){
- var list = [0, 1, 1, 3];
- Test.assertEquals(getMin(list), 1, "test fails");
- });
- it("test2", function(){
- var list = [7, 2, null, 3, 0, 10];
- Test.assertEquals(getMin(list), 2, "test fails");
- });
- it("test3", function(){
- var list = [null, -2];
- Test.assertEquals(getMin(list), 0, "test fails");
- });
- it("test4", function(){
- var list = [5,null, -2, {a:"5"}, [1,2,3],3];
- Test.assertEquals(getMin(list), 3, "test fails");
- });
- });