xxxxxxxxxx
finding the smallest number other than 0 in an array javascript
const arr = [14, 58, 20, 77, 0, 66, 82, 42, 67, 42, 4, 0]
const min = Math.min(arr.filter((n) => n > 0))
console.log(min)
// Output: 4
xxxxxxxxxx
const arr = [14, 58, 20, 77, 66, 82, 42, 67, 42, 4]
const min = Math.min(arr)
console.log(min)
xxxxxxxxxx
function sumTwoSmallestNumbers(numbers) {
numbers = numbers.sort((a, b) => {
return a - b; });
}; //this will turn the numbers list into the 2 lowest numbers
xxxxxxxxxx
//Not using Math.min:
const min = function(numbers) {
let smallestNum = numbers[0];
for(let i = 1; i < numbers.length; i++) {
if(numbers[i] < smallestNum) {
smallestNum = numbers[i];
}
}
return smallestNum;
};
xxxxxxxxxx
function smallestElemnts(numbers) {
var smallest = numbers[0];
for (let i = 0; i < numbers.length; i++) {
var elements = numbers[i];
if (elements < smallest) {
smallest = elements;
}
}
return smallest;
}
const numbers = [2, 3, 4, 8, 5, 2, 4];
console.log(smallestElemnts(numbers));
//Output: 2
xxxxxxxxxx
Array.min = function( array ){
return Math.min.apply( Math, array );
};