最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to generate a four digit code (i.e 0001) javascript - Stack Overflow

programmeradmin2浏览0评论

I need to autogenerate a new four digit code that everytime I run the function the value increments. Such as 0001 the first time, 0002, 0003 and so on. This is the code I have so far, the format is okay but I cant get it to increment the value automatically. I know it's probably a while loop but I wanted to ask advise on how it should be done. Thank you.

function pad(n){

var num = 1;
var string = "" + num;
var pad = "0000";
n = pad.substring(0, pad.length - string.length) + string;
num++;
return n;
}

I need to autogenerate a new four digit code that everytime I run the function the value increments. Such as 0001 the first time, 0002, 0003 and so on. This is the code I have so far, the format is okay but I cant get it to increment the value automatically. I know it's probably a while loop but I wanted to ask advise on how it should be done. Thank you.

function pad(n){

var num = 1;
var string = "" + num;
var pad = "0000";
n = pad.substring(0, pad.length - string.length) + string;
num++;
return n;
}
Share Improve this question asked Jun 5, 2016 at 21:37 GonzaloGonzalo 1603 silver badges16 bronze badges 3
  • you are defining your num inside the pad function, so every time you run pad, you will get a new num equal to 1. you should define num somewhere outside your function, or even better, supply the num as a parameter to the function. You don't need to provide n as a parameter, since it is the return value of the function already. – Claies Commented Jun 5, 2016 at 21:41
  • Not a solution, but you might want to consider renaming the variable string as its a reserved word. – NickSlash Commented Jun 5, 2016 at 21:43
  • how to increment the number when the button is click ? i have tried it with a button but it won't work. – Seher Mahmud Commented Sep 19, 2021 at 12:51
Add a ment  | 

4 Answers 4

Reset to default 7

You should use left-pad for this Joke aside you can avoid polluting the global scope with a closure:

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

Usage:

console.log(pad()); // "0001"
console.log(pad()); // "0002"
console.log(pad()); // "0003"

you need to keep the value of num outside function scope

otherwise, it will be 1 every time you invoke it

here is the solution:

var num = 1;

function pad(n){
    var string = "" + num;
    var pad = "0000";
    n = pad.substring(0, pad.length - string.length) + string;
    num++;
    return n;
}

also, to make it more reusable, you can do the following (which I think you intended to do):

var num = 1;

function pad(n){
    var string = "" + n;
    var pad = "0000";
    n = pad.substring(0, pad.length - string.length) + string;
    return n;
}

pad(num++); // 0001
pad(num++); // 0002

You could use Number.prototype.toLocaleString(locales, options).
In particular, its minimumIntegerDigits option makes you able to left-pad numbers to the required number of digits :

var number = 12;
number.toLocaleString(undefined, {useGrouping: false, minimumIntegerDigits: 4})

This will display 0012.

Leaving the locales undefined tells the function to work with the OS's locale, which should always be ok since we switched off grouping and aren't displaying currencies nor decimal numbers.

useGrouping is the only option whose default doesn't work well with your need, if left unspecified it will instead display 0 012 for my locale (fr_FR) and probably 0,012 for the en_US.

UPD: I like andlrc's solution. It's more JS way than my =)

===

better solution - without global variable (OOP style)

function ID(start){
  this.num = start || 0;
}

ID.prototype._to4DigitString = function(){
  return ("0000"+this.num).slice(-4);
}

ID.prototype.current = function(){
  return this._to4DigitString(this.num);
}

ID.prototype.next = function(){
  return this._to4DigitString(++this.num);
}

//then you can use it
var id = new ID();
id.next();    // "0001"
id.current(); // "0001"
id.next();    // "0002"
发布评论

评论列表(0)

  1. 暂无评论