4 Digit Number Generator List
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
881k 121 gold badges 1179 argent badges 1293 bronze badges
asked Aug ii at ten:58
2
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
ulouulou
5,197 5 gilt badges 33 argent badges 43 bronze badges
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
TSRTSR
14.3k 22 gilded badges 76 silver badges 162 bronze badges
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
paripari
26 2 bronze badges
4 Digit Number Generator List,
Source: https://stackoverflow.com/questions/73206387/how-to-generate-4-digit-number-from-0000-to-9999-using-js
Posted by: burgessnatch1943.blogspot.com
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 usedpad()
once.Aug 2 at 11:02