-1
A
回答
0
下面的代碼將生成一個令牌,您可以前進一個。它使用一些小類來實現模塊化。您附加到該按鈕的功能將調用標記的下一個功能,如底部所示。
//Element in a big token
function Increment(startval, endval) {
this.start = startval.charCodeAt(0);
this.cur = this.start;
this.end = endval.charCodeAt(0);
//Returns the current value
this.get = function() {
if (this.cur <= this.end) {
return String.fromCharCode(this.cur);
} else {
return null;
}
}
//Advances the value we will show
this.next = function() {
this.cur += 1;
return this.get();
}
//Reset it to the beginning
this.reset = function() {
this.cur = this.start;
}
}
function Token() {
this.token = [
new Increment("A", "Z"),
new Increment("A", "Z"),
new Increment("0", "9"),
new Increment("0", "9"),
new Increment("0", "9")
];
this.get = function() {
return this.token.map(function(cur) {
return cur.get();
}).join("");
}
this.next = function() {
//From the back to the front
for (var i = this.token.length - 1; i >= 0; i--) {
if (this.token[i].next() == null) {
//If we exhausted all possible values, continue
this.token[i].reset();
} else {
//Until we advance one that has still values left
break;
}
}
return this.get();
}
}
//Now we are just showing off...
var a = new Token();
for (var i = 0; i < 15; i++) {
console.log(a.next());
}
0
const getNextAlphaString = function (str) {
"use strict";
str=str.toUpperCase();
let chars;
chars = str.split("");
const strLen = str.length;
let continueIncermenting = true;
for (let i = strLen - 1; i >= 0; i = i - 1) {
let asciiVal;
asciiVal = chars[i].charCodeAt(0);
if (isNaN(asciiVal)) {
return str;
}
if (continueIncermenting === true) {
if (asciiVal >= 48 && asciiVal < 57) {
chars[i] = String.fromCharCode(asciiVal + 1);
continueIncermenting = false;
break;
} else if (asciiVal == 57) {
chars[i] = '0';
continueIncermenting = true;
}
if (asciiVal >= 65 && asciiVal < 90) {
chars[i] = String.fromCharCode(asciiVal + 1);
continueIncermenting = false;
break;
} else if (asciiVal == 90) {
chars[i] = String.fromCharCode(65);
continueIncermenting = true;
}
} else {
if (asciiVal == 90) {
continueIncermenting = true;
chars[i] = String.fromCharCode(65);
}
if (asciiVal == 57) {
continueIncermenting = true;
chars[i] = '0';
}
}
}
if (continueIncermenting === true) {
let firstAcii = chars[0].charCodeAt(0);
if (isNaN(firstAcii)) {
return str;
}
if ((firstAcii >= 65 && firstAcii <= 90) || (firstAcii >= 97 && firstAcii <= 122)) {
return 'A' + chars.join('').toUpperCase();
}
if (firstAcii >= 48 && firstAcii <= 57) {
return '0' + chars.join('').toUpperCase();
}
}
return chars.join('').toUpperCase();
};
結果將是如下:
ab1zde ---> AB1ZDF
abcdzz ---> ABCEAA
ZZZZZ ---> AAAAAA
abcyzz ---> ABCZAA
9000 ---> 9001
1
這不使用任何'聰明'的技巧,但更短,更容易理解和調整。它也使用數字,因此它不會像字符串一樣快速地增長字符串的長度。它從0到9然後是a-z遞增,但是它意味着你應該以'a'開始。輸出總是以字母字符開始,只要你輸入也以字母字符開始,因此它可以作爲PHP變量名
var nextVarName = function(str) {
// Position of char to change.
var change = str.length - 1;
// The value of that char.
var change_char = str[change];
// Number of zeros to append when flipping.
var zeros = 0;
// Iterate backwards while there's a z (flipping).
while (change_char == 'z') {
// Increase the length of appended zeros
zeros++;
// Move the char to change back.
change_char = str[--change];
}
if (change_char == undefined) {
// Full flip - string increases in length.
str = 'a' + Array(str.length + 1).join("0");
} else {
// Normal increment with partial flip and 9->a handling.
str = str.substr(0, change)
+ (change_char == '9' ? 'a' : String.fromCharCode(str.charCodeAt(change) + 1))
+ Array(zeros + 1).join('0');
}
return str;
};
var vname = 'a';
for (var i = 0; i < 5; i++) {
vname = nextVarName(vname);
console.log(vname);
}
結果將如下:
ž - - > A0
9Z ---> A0(意外輸入)
ab1zde ---> ab1zdf
abcdzz ---> abce00
ZZZZZ ---> A00000
abcyzz ---> abcz00
9000 ---> 9001(意外輸入)
https://jsfiddle.net/e20kfvky/
多少變量的時間表(即創建的每個長度如下:
1:26,2:936,3:33696,4:1213056 ... n:36^n - 10 * 36^(n - 1)
相關問題
- 1. 自動遞增的數字
- 2. 在javascript中遞增字母
- 3. 遞增字母數字
- 4. 自動遞增的主字母數字鍵
- 5. 遞增的字母數字字符串
- 6. 自動遞增字母數字字符Id
- 7. 自動增量字母數字(字母數字++)值在C#.net
- 8. MySql自動遞增字母數字主鍵?
- 9. 用一串數字和字母自動遞增
- 10. 如何自動遞增ID號與字母和數字
- 11. 使用字母數字自動遞增值
- 12. c中字母的遞增#
- 13. 非典型字母和數字遞增
- 14. Php如何自動遞增字符?
- 15. Java中的「自動增量」字母表?
- 16. 自動增加JavaScript中的數字
- 17. 在字母數字字符串末尾的javascript增量數字
- 18. MySQL自動增加加上一列中的字母數字
- 19. 連續字母數字的自動增量。 (Python)
- 20. 自動增加表格ID與字母和數字的混合
- 21. 自動增加MSSQL中的字母數字ID
- 22. 如何在PHP中生成字母數字遞增ID?
- 23. 自動遞增字段不
- 24. Django/Python中的自動遞增字段
- 25. 如何增加PHP中的字母數字字符串?
- 26. 遞增的數字
- 27. TFS - WITT中的自動遞增數字字段(例如Bug's)
- 28. Mysql自動增量ID或字母數字ID作爲外鍵
- 29. Last_insert_id爲字母和數字自動增量列返回0
- 30. 複合(字母數字)主鍵和自動增量
你怎麼做到的? –
你是如何獲得按鈕按下的第一個值? –
你忘了問我們一個問題,或者告訴我們你目前所做的最佳努力的任何代碼。此外,你沒有解釋你當前的代碼在哪裏失敗,或者*如何失敗。您是否希望我們幫助解決您的問題,或只是以某種方式鼓勵您? –