The function should find the smallest (positive) number in the array.
The array can contain the following values:
- Positive number
- Negavitve number
0
-
Null
value
If no number is found, you should return 0
Note: Zero is also counted as a positive number.
#Examples
[1, 2, 3, 4] => 1
[-2, 0, 4] => 0
[-2, -5] => 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 = [Number.MAX_VALUE]; Test.assertEquals(getMin(list), Number.MAX_VALUE, "test fails"); }); it("test5", function(){ var list = [0.5,7, 2, null, 3, 0, 10]; Test.assertEquals(getMin(list), 0.5, "test fails"); }); it("dynamic test", function(){ var minValue = null; var list = []; for(var i = 20; i > 0; i--){ var toAdd = (Math.random()*1000 - 500); if(toAdd >= 0 && (minValue === null || minValue > toAdd)) minValue = toAdd; list.push(minValue); } Test.assertEquals(getMin(list), minValue, "test fails"); }); it("long dynamic test", function(){ var minValue = null; var list = []; for(var i = 100; i > 0; i--){ var toAdd = (Math.random()*1000 - 500); if(toAdd >= 0 && (minValue === null || minValue > toAdd)) minValue = toAdd; list.push(minValue); } Test.assertEquals(getMin(list), minValue, "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 = [Number.MAX_VALUE];
- Test.assertEquals(getMin(list), Number.MAX_VALUE, "test fails");
- });
- it("test5", function(){
- var list = [0.5,7, 2, null, 3, 0, 10];
- Test.assertEquals(getMin(list), 0.5, "test fails");
- });
- it("dynamic test", function(){
- var minValue = null;
- var list = [];
- for(var i = 20; i > 0; i--){
- var toAdd = (Math.random()*1000 - 500);
- if(toAdd >= 0 && (minValue === null || minValue > toAdd))
- minValue = toAdd;
- list.push(minValue);
- }
- Test.assertEquals(getMin(list), minValue, "test fails");
- });
- it("long dynamic test", function(){
- var minValue = null;
- var list = [];
- for(var i = 100; i > 0; i--){
- var toAdd = (Math.random()*1000 - 500);
- if(toAdd >= 0 && (minValue === null || minValue > toAdd))
- minValue = toAdd;
- list.push(minValue);
- }
- Test.assertEquals(getMin(list), minValue, "test fails");
- });
- });