title: Write Number in Expanded Form
body: <p>I am doing some code exercise, it is about to create a function <code>expandedForm</code> to take a number parameter, the example in below would be more clear..</p>
  <pre><code>expandedForm(12); // Should return '10 + 2'
  expandedForm(42); // Should return '40 + 2'
  expandedForm(70304); // Should return '70000 + 300 + 4'
  </code></pre>
  <p>My solution:</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 expandedForm(num) {
    // Your code here
    let numStr = num.toString().split('');
    
    for(let i = 0 ; i &lt; numStr; i++ ){
        
        for(let y = numStr.length; y &gt; 1; y--){
           numStr[i] += '0'; 
           // console.log(y);  use this to debug y, and no y value print out from console
        }
    }
    
    return numStr.join('+')
  }
  
  console.log(expandedForm(23));</code></pre>
  </div>
  </div>
  </p>
  <p>if I test expandedForm(23), the result is '2+3', also the y value hasn't print to the console, can anyone tell me what wrong with my solution? thank you.</p>
  <hr />
  <h1>Solution</h1>
  <p>Thanks everyone, thanks for pointing out my <code>variable y</code> initial in for loop is not correct, and <code>i &lt; numStr</code> as well (silly mistake).</p>
  <p>After review my code and inspired by some suggestions below, my final solution is</p>
  <pre><code> function expandedForm(num) {
        // Your code here
        let numStr = num.toString().split('');
        
        for(let i = 0 ; i &lt; numStr.length; i++ ){
            
            for(let y = numStr.length - i; y &gt; 1; y--){
               numStr[i] += '0'; 
               // console.log(y);  use this to debug y, and no y value print out from console
            }
        }
        
       
        numStr = numStr.filter(value =&gt; !value.startsWith(0));
        return numStr.join(' + ')
      }
  
      console.log(expandedForm(23));
  </code></pre>