I desire to print sequential number from 0000 to 9999.

This code simply print 0000

                  var pad = (function(num) {   return function() {     var str = Cord(num++);     while (str.length < 4) str = "0" + str;     render str;   } })(1);                

from here How to generate a four digit code (i.due east 0001) javascript

I did some changes but did't piece of work with me:

                  var j = 0; for (j; j <= 9999; j++) {   var pad = (part(num) {     return function() {        var str = String(num++);       while (str.length < four)         str = "0" + str;       return str;     }   })(0);   console.log('Loop', j); } console.log('Number', pad());                

Quentin's user avatar

Quentin

881k 121 gold badges 1179 argent badges 1293 bronze badges

asked Aug ii at ten:58

shahid's user avatar

2

  • Can you explain 'what's non working for me'?

    Aug ii at xi:00

  • You lot replaced the value of pad 10000 times, merely only used pad() once.

    Aug 2 at 11:02

3 Answers iii

One liner using Array.from:

                  const generatePaddedNums = (n) => Array.from(   { length: northward },   (_, i) => i.toString().padStart((n - i).toString().length, '0') ) console.log(generatePaddedNums(10000))                
                  .equally-panel-wrapper { max-tiptop: 100% !important; top: 0; } /* ignore this */                

answered Aug 2 at 11:06

ulou's user avatar

Y'all should put the print pad() within the loop and the function pad declared only once before the loop

                  var j = 0; var pad = (function(num) {   return function() {     var str = Cord(num++);     while (str.length < 4) str = "0" + str;     return str;   } })(1);  for (j; j <= 9999; j++) {   console.log('Number',pad())   panel.log('Loop', j); }                

answered Aug ii at 11:09

TSR's user avatar

you have to call pad() role inside for Loop

Refer below code

                var pad = (part(num) {     return part() {         var str = String(             num++);         while (str.length <             4)             str = "0" + str;         return str;     } })(1);  for (var i = 0; i < 9999; i++) {     console.log('Number', pad()); }                              

answered Aug 2 at xi:16

pari's user avatar