title: How to use array.push inside a function
body: <p>I'm trying to return the array an array of numbers that conform to the two if statements. The prompt came from leet code, "Self Dividing Numbers", and asks to take in two arguments, a lower and upper bound and check if whether or not each number in that range is divisible by the digits of each individual number.</p>
  
  <p>When I console.log(num) (the commented out portion, I get a correct list of numbers, but not in an array format. To fix this I thought to add a variable, result and return result after pushing an array to result inside the for loop. However when I do this, i only get the first correct term in an array, but not the full array. </p>
  
  <p>How can this be fixed? I've tried moving the return statement in various locations, but that did not fix the issue. </p>
  
  <p>The function should return [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22];</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function selfDividingNumbers(left, right) {
  
      for (let j = left; j &lt;= right; j++) {
          let num = j;
          let result = []
          let strNum = num.toString();
          let dividingDigit = 0;
  
          for (let i = 0; i &lt; strNum.length; i++) {
              if (num % parseInt(strNum[i]) == 0) {
                  dividingDigit++;
              }
              if (dividingDigit == strNum.length) {
                  result.push(num)
                  //console.log(num)
              }
          }
          return result
      }
  };
  
  console.log(selfDividingNumbers(1, 22));</code></pre>
  </div>
  </div>
  </p>