- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing

- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM

- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers

- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources

# JavaScript Program to Find Maximum value possible by rotating digits of a given number

We will be writing a program to find the maximum value possible by rotating the digits of a given number. We will be using a loop to break down the number into individual digits and rearrange them in a way that gives us the maximum value. The loop will continuously rotate the digits and keep track of the highest value obtained until all possible rotations have been evaluated. The maximum value obtained from this process will then be returned as the result.

## Approach

To find the maximum possible value by rotating digits of a given number, follow these steps −

Convert the number to a string to access its individual digits.

Create an array of all possible rotations of the number.

Sort the array in non-ascending order.

Convert the largest element in the array back to a number.

Return the largest number.

To handle negative numbers, the sign of the largest number should be determined before converting it back to a number.

## Example

Here is an example of a JavaScript program to find the maximum value possible by rotating the digits of a given number −

function maxRotate(num) { num = num.toString(); let max = num; for (let i = 0; i < num.length - 1; i++) { num = num.slice(0, i) + num.slice(i + 1) + num[i]; if (num > max) { max = num; } } return max; } console.log(maxRotate(38596));

## Explanation

The function

**maxRotate**takes in a number**num**as an argument and converts it to a string.A variable

**max**is declared and is assigned the value of**num**. This variable will store the maximum value possible by rotating the digits of**num**.A for loop is used to iterate through the digits of

**num**. For each iteration, the**num**string is reassembled by removing the digit at index**i**, concatenating the remaining digits, and then adding the removed digit back to the end of the string.After each iteration, the value of

**num**is compared with the value of**max**. If**num**is greater than**max**, the value of**num**is assigned to**max**.Finally, the function returns the value of

**max**after all iterations are completed. In this example, when the function is called with the argument**38596**, the returned value is**956638**, which is the maximum value possible by rotating the digits of**38596**.

- Related Articles
- Obtaining maximum number via rotating digits in JavaScript
- JavaScript Program for Queries to find the maximum sum of contiguous subarrays of a given length in a rotating array
- C++ program to find maximum possible value of XORed sum
- Program to find maximum possible value of an expression using given set of numbers in Python
- Program to find maximum possible value of smallest group in Python
- C++ program to find maximum possible value for which XORed sum is maximum
- Find maximum number that can be formed using digits of a given number in C++
- JavaScript Program to Check if all array elements can be converted to pronic numbers by rotating digits
- How to count digits of given number? JavaScript
- Write a Golang program to find the sum of digits for a given number
- JavaScript Program to Check if it is possible to make array increasing or decreasing by rotating the array
- Program to find minimum possible maximum value after k operations in python
- C++ program to find out the maximum possible tally from given integers
- C++ Program to Sum the digits of a given number
- The sum of digits of a two-digit number is 15. The number obtained by reversing the order of digits of the given number exceeds the given number by 9. Find the given number.