javascript - for loop for 0 to n number with digits specification -
how can write loop 16 digit number 0 n
i want output like:
0000000000000000 0000000000000001 0000000000000002 0000000000000003 0000000000000004 0000000000000005
tried isn't working:
for(i = 0000000000000000; < 0000000000000010; i++){ $("#testdiv").append(i); }
check out jsfiddle
<!doctype html> <html> <head lang="en"> <meta charset="utf-8"> <title></title> </head> <body> <script> function writenumber(n){ var nlen = n.tostring().length; var zerolen = 16-nlen; var arr = []; var str = "0"; for(var z=0;z<zerolen;z++){ arr.push(str); } arr.push(n.tostring()); return arr.join(''); } window.onload = function () { var arr = []; for(i=0;i<11;i++){ var str = writenumber(i); arr.push(str); } document.getelementbyid("testdiv").innerhtml = arr.join('<br>'); }; </script> <div id="testdiv"></div> </body> </html>
more efficient array. maybe doesn't matter when loop 10 items, you're going write dom 10 times if access dom inside loop instead of outside loop 1 time. small things adds up, may matter.
Comments
Post a Comment