Using array inside recursive function - Javascript Array Operation

Javascript examples for Array Operation:Array Element

Description

Using array inside recursive function

Demo Code

ResultView the demo in separate window

<html>
   <head></head>
   <body> 
      <script>
function factor(number, divisor, arr) {
  arr = arr || [];//from w ww. j ava2 s.co m
  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