Finding tidy numbers - JavaScript


A tidy number is a number whose digits are in non-decreasing order. We are required to write a JavaScript function that takes in a number and checks whether its a tidy number or not.

For example −

489 is a tidy number
234557 is also a tidy number
34535 is not a tidy number

Example

Following is the code −

const num = 234789;
const isTidy = (num, last = 10) => {
   if(num){
      if(num % 10 > last){
         return false;
      };
      return isTidy(Math.floor(num / 10), (num % 10));
   };
   return true;
};
console.log(isTidy(num));

Output

Following is the output in the console −

true

Updated on: 16-Sep-2020

154 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements