Find the factor for an integer number - Javascript Data Type

Javascript examples for Data Type:Number

Description

Find the factor for an integer number

Demo Code

ResultView the demo in separate window

<html>
   <head></head>
   <body> 
      <script>
function factor(number, divisor, arr) {
  arr = arr || [];/*  w w w.  java  2s.com*/
  if (divisor >= 1) {
    if (number % divisor === 0) {
      arr.push(divisor);
      return factor(number, divisor - 1, arr);
    } else {
      return factor(number, divisor - 1, arr);
    }
  }
  return arr;
}
 console.log("factor of a number 36");
 console.log(factor(36, 36));

      </script>  
   </body>
</html>

Related Tutorials