how to find the smallest number in an array code example

Example 1: javascript find smallest number in an array

const arr = [14, 58, 20, 77, 66, 82, 42, 67, 42, 4]
const min = Math.min(...arr)
console.log(min)

Example 2: find smallest number in array js

//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;
};

Example 3: how to find smallest number in array js

Array.min = function( array ){
    return Math.min.apply( Math, array );
};

Example 4: java find biggest number in array

for (int counter = 1; counter < decMax.length; counter++)
{
     if (decMax[counter] > max)
     {
      max = decMax[counter];
     }
}

System.out.println("The highest maximum for the December is: " + max);

Example 5: finding the smallest number in an array javascript

var arr = [5,1,9,5,7];
var smallest = arr[0];

for(var i=1; i<arr.length; i++){
    if(arr[i] < smallest){
        smallest = arr[i];   
    }
}

console.log(smallest);

Tags: