Created
January 10, 2023 12:39
-
-
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".
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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