Question: I need your help.

I'd like to create a function that would add some zeros in front of a number. The maximum numbers of digits that the total string should have is 6. Here are examples:

```
     9 -> 000009
    14 -> 000014
   230 -> 000230
  1459 -> 001459
 21055 -> 021055
987632 -> 987632 (Do nothing, there's already 6 digits)

```

Answer: 
A simple one line solution without any loops
--------------------------------------------

Works with IE5-11, Firefox, Chrome, etc. Assumes integer input.

```
 function pad(n) { return ("000000" + n).slice(-6); }

```

**Run snippet to test:**

```html
<html>
<body>
<input id="stdin" placeholder="enter a number" maxlength="6"><button onclick="test()">Test</button>
<textarea id="stdout" style="width:100%;height:20em;padding:1em;"></textarea>
<script type="text/javascript">

   function pad(n) { return ("000000" + n).slice(-6); }

   function test() {
       var n = parseInt( document.getElementById('stdin').value);
       var e = document.getElementById('stdout').innerHTML += n + ' = ' + pad(n) + '\n';
   }

</script>
</body>
</html>
```
You will need to convert the number to a string. Then I would split that string to an array and then add '0' to the front of that array until the length is 6. Then join. Check out this repl or see the code below: <http://repl.it/piT>

```
var num = 14;
var lengthOfNum = 6;
var numString = num.toString();
var numArray = numString.split('');
var returnString = '';

while (numArray.length < 6) {
    numArray.unshift('0');
}

returnString = numArray.join('');

```