How to use Math.max Method In Javascript
The Math.max method is used to find the largest number from the given numbers. We are randomly finding the maximum digit from the input set of numbers and storing the result in a new variable. We are using the Math.max method to find the maximum digit from the set of numbers.
Syntax:
Math.max([value1[, value2[, ...]]])
Example: In this example, we will construct the largest number from the digits using the Math.max() method.
Javascript
let inputNumber = [8, 3, 4, 7, 9]; let largestNumber = "" ; while (inputNumber.length > 0) { let largestDigit = Math.max( ...inputNumber ); largestNumber += largestDigit.toString(); inputNumber.splice( inputNumber.indexOf( largestDigit ), 1 ); } console.log(largestNumber); |
98743
JavaScript Program to Construct Largest Number from Digits
In this article, we have given a set of digits, and our task is to construct the largest number generated through the combination of these digits. Below is an example for a better understanding of the problem statement.
Example:
Input: arr[] = {4, 9, 2, 5, 0}
Output: Largest Number: 95420
Table of Content
- Using sort() Method
- Using Math.max Method
- Using Loops
- Using Array