Back to Browse

Program 13 Find the Smallest number from an array | Math.min() | Array sort | Arrays

110 views
Apr 9, 2023
7:05

1. Using for loop:- ------------------- To find the smallest number in JavaScript from the given array [12, 09, 45, 78, 67, 09], you can use a for loop and comparison, as it has a time complexity of O(n) (linear time). This approach only requires iterating over the array once and comparing each element with the current smallest number. 2. Using Math.min():- ---------------------------- To find the smallest number in JavaScript from the given array [12, 09, 45, 78, 67, 09], you can use the Math.min() method along with the spread operator ... to convert the array elements to a list of arguments. 3. Sort:- -------- the sort() method is used to sort the array in ascending order. The sort() method takes a compare function as a parameter, which determines the order in which the elements are sorted. The compare function subtracts a from b to sort the elements in ascending order. After the array is sorted, the smallest number is assigned to the minValue variable, which is the first element of the sorted array.

Download

0 formats

No download links available.

Program 13 Find the Smallest number from an array | Math.min() | Array sort | Arrays | NatokHD