2
I need to create a Javascript function that takes a string of letters and numbers and it returns the next count value. The count goes from 0 to Z. Example of the sequence:
0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - A - B - C - D [...] Y - Z - 01 - 02 - 03 [...] 0Y - 0Z - 10 - 11 - 12 [...]
Example of the function:
nextCode ("AF10") // Returns "AF11"
nextCode ("A4ZZ") // Returns "A500"
What I’ve tried so far:
/*Gerar Novo Código*/
function nextCode(lastCode){
//Verifica o Tamanho
var codeSize = lastCode.length;
//Verifica o index do ultimo caracter
var indexLastValidChar = codeSize-1;
//Se o ultimo caracter for igual a Z pega o caractere anterior (loop)
var loopAtive;
do {
indexLastValidChar = loopAtive == true ? indexLastValidChar-1 :indexLastValidChar;
var lastValidChar = lastCode.substr(indexLastValidChar, 1);
loopAtive = true;
} while(lastValidChar == "Z");
//Pega a primeira metade do novo código
var firstHalfCode = lastCode.slice(0, indexLastValidChar);
//Verifica se o ultimo caracter diferente de Z é numero ou letra
var isChar = isNaN(parseInt(lastValidChar));
//Se for char, gera o próximo caracter
var newChar;
if (isChar){
newChar = String.fromCharCode(lastValidChar.charCodeAt(0)+1);
} //Se não for, verifica se o número é 9
else if (lastValidChar == "9") {
newChar = "A";
} //Se não for, adicona um ao número
else {
newChar = parseInt(lastValidChar) + 1;
}
//Junta primeira metade
firstHalfCode = firstHalfCode + newChar;
//Adicona zeros ao final, caso codeSize seja diferente de indexLastValidChar+1 - ou seja, existem Zs
for (var i = 0; i < codeSize - indexLastValidChar+1; i++) {
var newCode = firstHalfCode + 0;
}
//Mostra o novo código
console.log(newCode);
}
nextCode ("AF10");
nextCode ("A4ZZ");
The results don’t work out and I’m a little lost...
Add to your question what you have so far.
– Chun
I added to the question !
– rodrilima
It seems to me that you are wanting to work on 36 basis. After Z would not see the value 10? It needs after Z to be even 01?
– Gabriel Katakura