Finding special type of numbers - JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

In the decimal number system, all the real numbers can be divided into two groups −

  • Rational Numbers
  • Irrational Numbers

For the scope of this problem we will only discuss the rational numbers,

All those numbers which can be written in the p/q (where q !== 0) form are called rational numbers.

Like 14, 4.6, 3.33333... and many more

The rational numbers, further can be divided into two groups −

  • Terminating decimal numbers
  • Repeating decimal numbers

This categorization is made on the basis of result obtained upon dividing p by q.

The thumb for this categorization is that −

  • We will obtain a terminating decimal number if and only if the prime factors of q are only 2 and 5
  • We will obtain a repeating decimal number of the prime factors of q includes any other number than 2 or 5.

So, we are required to write a JavaScript function that takes in a number that represents the value q. Our function should return true if for that value of we can obtain a terminating decimal number, false otherwise.


Following is the code −

const num = 1250;
const isTerminating = num => {
   while(num !== 1){
      if(num % 2 === 0){
         num /= 2;
      }else if(num % 5 === 0){
         num /= 5;
         return false;
   return true;


This will produce the following output in console −

Published on 30-Sep-2020 13:49:05