Answered
When given an array of numbers like below:
[54, 322, 12, 24, . . .]
How do I get the smallest number in the array?
I'm using JavaScript to do this.
A solution in one line of code:
array.sort()[0]
It first sorts the array from smallest to largest:
array.sort()
This puts the smallest item in the first index position, which is retrieved like this:
array[0]
You can loop through each value in the array and return the smallest one:
var array = [54, 322, 12, 24, 143, 343.32, 34.1]
var minValue = array[0]
for (var i=0; i < array.length; i++) {
if (array[i] < minValue) {
minValue = array[i]
}
}
// minValue = 12