
- 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
Just smaller number with monotone digits in JavaScript
Monotonically Increasing Digits
An integer has monotone increasing digits if and only if each pair of adjacent digits x and y satisfy x <= y.
Problem
We are required to write a JavaScript function that takes in a number, num, as the first and the only argument.
Our function should simply find the largest number that is less than or equal to num with monotone increasing digits.
For example, if the input to the function is
Input
const num = 332;
Output
const output = 299;
Example
Following is the code −
const num = 332; const monotoneIncreasingDigits = (num) => { const checkMonotone = (x) =>{ if (x <= 9) { return true } let currentDigit = x % 10 while (x) { const next = Math.floor(x / 10) const nextDigit = next % 10 if (currentDigit >= nextDigit) { currentDigit = nextDigit x = next } else { return false } } return true } if (checkMonotone(num)) { return num } const digits = num.toString().split('').map(x => Number(x)) return digits.reduce((acc, num, index) => { if (num >= 1) { const current = parseInt(digits.slice(0, index).join('') + num - 1 + new Array(digits.length - index - 1).fill('9').join(''), 10) if (checkMonotone(current)) { return Math.max( acc,current) } } return acc }, 0) } console.log(monotoneIncreasingDigits(num));
Output
299
- Related Articles
- Monotone Increasing Digits in C++
- Finding the just bigger number formed by same digits in JavaScript
- Returning number with increasing digits. in JavaScript
- Find largest number smaller than N with same set of digits in C++
- Fetch Numbers with Even Number of Digits JavaScript
- Smallest prime number just greater than the specified number in JavaScript
- Number of smaller and larger elements - JavaScript
- Numbers smaller than the current number JavaScript
- Finding product of Number digits in JavaScript
- Separating digits of a number in JavaScript
- Number Split into individual digits in JavaScript
- Finding the immediate bigger number formed with the same digits in JavaScript
- Number of digits that divide the complete number in JavaScript
- Find smallest number with given number of digits and sum of digits in C++
- Recursively adding digits of a number in JavaScript

Advertisements