Skip to content

Instantly share code, notes, and snippets.

@ianitsky
Created January 10, 2023 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianitsky/721a14eb0b0b85b265171d2c3b4904d6 to your computer and use it in GitHub Desktop.
Save ianitsky/721a14eb0b0b85b265171d2c3b4904d6 to your computer and use it in GitHub Desktop.
ChatGPT: Create a javascript function that receive a string and return the next string using only the following chars "346789ABCDEFGHJKLMNPQRTUVWXYZ". Example: for string "3" the next is "4". For string "34" the next is "36". For string "AAAA" the next is "AAAB". For string "AAAZ" the next is "AAB3".
function nextString(str) {
let chars = "346789ABCDEFGHJKLMNPQRTUVWXYZ";
let result = "";
let carry = true;
for (let i = str.length - 1; i >= 0; i--) {
let index = chars.indexOf(str[i]);
if (carry && index < chars.length - 1) {
result = chars[index + 1] + result;
carry = false;
} else if (carry && index === chars.length - 1) {
result = chars[0] + result;
carry = true;
} else {
result = str[i] + result;
}
}
return carry ? chars[0] + result : result; // If there is a carry, add a '1' to the beginning of the string. Otherwise, return the string as is.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment