Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This should do it:</p> <pre><code>function getNext(num) { var alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZ"; var digits = num.toUpperCase().split(""), len = digits.length, increase = true; if (len != 3) throw new Error("Invalid serial number length in getNext: "+num); for (var i=len-1; increase &amp;&amp; i&gt;=0; i--) { var val = alphabet.indexOf(digits[i]); if (val == -1) throw new Error("Invalid serial number digit in getNext: "+num); val++; if (val &lt; alphabet.length) { digits[i] = alphabet[val]; increase = false; } else { // overflow digits[i] = alphabet[0]; } } if (increase) // is still true throw new Error("Serial number overflow in getNext"); num = digits.join(""); return num; } </code></pre> <p>Since you are working with a nearly alphanumeric alphabet, a <code>parseInt</code>/<code>toString</code> with radix 33 might have done it as well. Only you need to "jump" over the <code>0</code>, <code>I</code> and <code>O</code>, that means replacing <code>0,A,B…</code> by <code>A,B,C…</code>, replacing <code>H,I,J…</code> by <code>J,K,L…</code> and replacing <code>M,N,O…</code> by <code>P,Q,R…</code> (and everything back on deserialisation) - which might be OK if JS has a numeric <code>char</code> datatype, but I think it's easier to do it manually as above.</p> <p>If you're curious:</p> <pre><code>String.prototype.padLeft = function(n, x) { return (new Array(n).join(x || "0")+this).slice(-n); }; function getNext(num) { var alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZ"; var back = {}, forth = {}; for (var i=0; i&lt;alphabet.length; i++) { var a = alphabet[i], b = i.toString(36); back[a] = b; forth[b] = a; } return (parseInt(num.replace(/./g, function(c) { return back[c]; // base33 from alphabet }), alphabet.length) + 1) .toString(alphabet.length) .padLeft(3) .replace(/./g, function(c) { return forth[c]; // base33 to alphabet }); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload