Question:
Given an array of integers, your solution should find the smallest integer.
For example:
- Given
[34, 15, 88, 2]
your solution will return2
- Given
[34, -345, -1, 100]
your solution will return-345
You can assume, for the purpose of this problem, that the supplied array will not be empty.
Solution A:
function findSmallest(a) { return a.sort(function (a,b) { return a - b; })[0]; } Solution B:
function findSmallest(a) { var s = a[0]; for(var i=1; i<a.length; i++) { if(s - a[i] > 0) { s = a[i]; } } return s; }
findSmallest([34, 15, 88, 2]); // Output 2
findSmallest([34, -345, -1, 100]); // Output -345
This question is referred from Codewars