- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements