Was it helpful?

Question

Tidy numbers in JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

A tidy number is a number whose digits are in non-decreasing order.

For example −

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

We are required to write a JavaScript function that takes in a number and checks whether its a tidy number or not.

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

This will produce the following output on console −

true
raja
Published on 01-Oct-2020 14:02:33
Advertisements
Was it helpful?
Not affiliated with Tutorialspoint
scroll top