Syntax error Check for Ugly number in JavaScript

Check for Ugly number in JavaScript



In the decimal number system, ugly numbers are those positive integers whose only prime factors are 2, 3 or 5.

For example − Integers from 1 to 10 are all ugly numbers, 12 is an ugly number as well.

Our job is to write a JavaScript function that takes in a Number and determines whether it is an ugly number or not.

Let's write the code for this function −

Example

const num = 274;
const isUgly = num => {
   while(num !== 1){
      if(num % 2 === 0){
         num /= 2;
      } else if(num % 3 === 0) {
         num /= 3;
      } else if(num % 5 === 0) {
            num /= 5;
      } else {
         return false;
      };
   };
   return true;
};
console.log(isUgly(num));
console.log(isUgly(60));
console.log(isUgly(140));

Output

The output in the console will be −

false
true
false
Updated on: 2020-08-31T06:59:27+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements