
- 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
Sorting an integer without using string methods and without using arrays in JavaScript
We are required to write a JavaScript function that takes in a number. The function should return the smallest number that can be formed rearranging the digits of the number.
For example −
If the input number is −
const num = 614532;
Then the output should be −
const output = 123456;
The only condition is that we can neither use any String methods nor any array to store data.
Example
The code for this will be −
const num = 614532; const sortDigits = num => { const getDigit = e => Math.floor(num / 10 ** e) % 10; const l = Math.ceil(Math.log10(num)) − 1; let e = l; while (e−−) { const left = getDigit(e + 1); const right = getDigit(e); if (left <= right){ continue; }; num += (right − left) * 9 * 10 ** e; e = l; }; return num; } console.log(sortDigits(num));
Output
And the output in the console will be −
123456
- Related Articles
- Reverse digits of an integer in JavaScript without using array or string methods
- Sorting Array without using sort() in JavaScript
- How to convert a string into an integer without using parseInt() function in JavaScript?
- How to sorting an array without using loops in Node.js?
- Golang Program to merge two integer arrays without using library function
- Add number strings without using conversion library methods in JavaScript
- Sorting arrays using bubble sort in JavaScript
- How can we sort a string without using predefined methods in Java?
- Fetch Second minimum element from an array without sorting JavaScript
- Converting number of corresponding string without using library function in JavaScript
- Count unique elements in array without sorting JavaScript
- Sorting string alphabetically and inserting underscores using JavaScript
- Java string length without using length() method.
- Check if items in an array are consecutive but WITHOUT SORTING in JavaScript
- Sorting binary string having an even decimal value using JavaScript

Advertisements