1

I need a function in JavaScript (for use in Node) that increments numbers in a string as follows:

"0", "1", "2", "3", ..., "8", "9", "00", "01", "02"

and so on. How would I go about doing this? I can think of a long way with lots of conditionals, but that probably won't run optimally.


What I have so far:

var count = "0";

function increment() {
    var number = parseInt(count), digits = count.length;
    var upDigit = true;
    for (var i = 0; i < digits; i++) {
        if (i !== 9)
            upDigit = false;
    }
    if (upDigit) {
        var zeros = "";
        for (var i = 0; i <= digits; i++) {
            zeros += "0";
        }
        count = zeros;
    } else {
        count = number++;
    }
}
4

5 回答 5

2

根据我之前的回答:

function range(i, to) {
    var v,
        r = [],
        s = to,
        e = i.length,
        t = Math.pow(10, e),
        l = (t + '').length;
    i = parseInt(i);
    to = Math.pow(10, to.length);
    for (; i <= to; i++) {
        if (i === t) {
            i = 0;
            t = Math.pow(10, ++e);
            l = (t + '').length;
        }
        v = new Array(l - (i + '').length).join('0') + i;
        r.push(v);
        if (v === s) { break; }
    }
    return r;
}

用法示例:

console.log(range('05', '15').join()); // "05,06,07,08,09,10,11,12,13,14,15"
于 2013-10-13T08:31:55.000 回答
1
function zeroPad(str, ln){
  if(str.length > ln){ raise('Trying to zero pad a too-long string') };
  var out = str;
  while(out.length !== ln){
    out = '0'+out;
  };
  return out;
}

function iterate(previous, final){
  console.log(previous);
  if(previous === final){ return };
  var next = "";
  var ln = previous.length; // how many characters so far
  // use a regex to test if every character is 9
  var nines = 
    previous.match(/^9+$/) ? true : false;
  if(nines){
    for(var i=0; i<ln+1; i++){
      next += "0";
    }
    return iterate(next, final);
  } else {
    var value = +previous; // coerce to number
    value += 1;
    next = zeroPad(String(value), ln)
    return iterate(next, final);
  }
};

iterate("0", "000");
于 2013-10-10T16:35:53.950 回答
1

这是你期待的结果吗?

var e = 0,
    t = 0,
    l = 0,
    i = 0;
for (; i <= 100; i++) {
    if (i >= t) {
        i = 0;
        t = Math.pow(10, ++e);
        l = (t + '').length;
    }
    console.log(new Array(l - (i + '').length).join('0') + i);
}

填充方法:

new Array(2).join('0') == ['', ''].join('0') == '0'; // true
于 2013-10-12T15:51:56.310 回答
0
function pad(num) {
  return num > 0 ? pad(num-1) + "0" : ""; 
}

function add(str) {
    var len, num, numlen;
    len = str.length;
    num = +str;
    numlen = String(num).length;
    if (/^9+$/.test(num)) { return pad(len + 1); }
    else { return pad(len - numlen) + String(num + 1); }
}

add('0078') // '0079'
add('0099') // '00000'

小提琴

于 2013-10-10T16:45:50.293 回答
-2

好吧,您可以尝试先将字符串转换为整数,然后加一,然后返回字符串,如下所示:

function incrementString(string){
    var number = parseInt(string);
    number++;
    return number.toString();
}
于 2013-10-10T16:33:12.830 回答