Back to Browse

Program 12 Find the biggest number from an array | For loop | Math.max() | Number sort

107 views
Apr 8, 2023
9:42

For loop is used to iterate over the array elements. The largestNumber variable is initialized with the first element of the array, and then each subsequent element is compared with it. If an element is larger than the current largest number, it is assigned to the largestNumber variable. Math.max():- --------------------- To find the largest number in JavaScript from the given array [12, 09, 45, 78, 67, 09], you can use the Math.max() method along with the spread operator ... to convert the array elements to a list of arguments. Sort:- -------- the sort() method is used to sort the array in descending 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 b from a to sort the elements in descending order. After the array is sorted, the largest number is assigned to the largestNumber variable, which is the first element of the sorted array. Finally, the largest number is logged to the console using console.log().

Download

0 formats

No download links available.

Program 12 Find the biggest number from an array | For loop | Math.max() | Number sort | NatokHD