{"record":{"id":"6f89cddf45ee89b8","repo":"TheAlgorithms/JavaScript","slug":"not-a-valid-character-digit","errorCode":null,"errorMessage":"Not a valid character: ${digit}","messagePattern":"Not a valid character: (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Conversions/ArbitraryBase.js","lineNumber":51,"sourceCode":"\n  const baseOneCharacters = [...baseOneCharacterString]\n  const baseTwoCharacters = [...baseTwoCharacterString]\n\n  for (const charactersInBase of [baseOneCharacters, baseTwoCharacters]) {\n    if (charactersInBase.length !== new Set(charactersInBase).size) {\n      throw new TypeError(\n        'Duplicate characters in character set are not allowed'\n      )\n    }\n  }\n  const reversedStringOneChars = [...stringInBaseOne].reverse()\n  const stringOneBase = baseOneCharacters.length\n  let value = 0\n  let placeValue = 1\n  for (const digit of reversedStringOneChars) {\n    const digitNumber = baseOneCharacters.indexOf(digit)\n    if (digitNumber === -1) {\n      throw new TypeError(`Not a valid character: ${digit}`)\n    }\n    value += digitNumber * placeValue\n    placeValue *= stringOneBase\n  }\n  const outputChars = []\n  const stringTwoBase = baseTwoCharacters.length\n  while (value > 0) {\n    const [divisionResult, remainder] = floorDiv(value, stringTwoBase)\n    outputChars.push(baseTwoCharacters[remainder])\n    value = divisionResult\n  }\n  return outputChars.reverse().join('') || baseTwoCharacters[0]\n}\n\n/**\n * Converts a arbitrary-length string from one base to other. Doesn't lose accuracy.\n * @param {string} stringInBaseOne String in input base\n * @param {string} baseOneCharacters Character set for the input base","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Conversions/ArbitraryBase.js#L33-L69","documentation":"Thrown by convertArbitraryBase during digit decoding when a character in stringInBaseOne is not present in baseOneCharacterString (indexOf returns -1). The interpolated ${digit} names the offending character. It protects the arithmetic from producing a silently wrong value (indexOf -1 would otherwise be treated as the last digit).","triggerScenarios":"Calling convertArbitraryBase('1A', '0123456789', '01') — 'A' is not in the decimal alphabet; passing lowercase digits into an uppercase alphabet; invisible characters like a leading/trailing space or a zero-width space sneaking into the input string.","commonSituations":"Mismatched case between input and alphabet (e.g. hex input 'ff' against alphabet '0123456789ABCDEF'); whitespace from untrimmed user input; copy-paste introducing a non-breaking space; using the wrong source alphabet for the data.","solutions":["Trim and normalize the input string's case to match baseOneCharacterString before calling.","Verify each character of the input exists in the source alphabet with a pre-check.","Re-confirm the source alphabet actually contains all glyphs that appear in your input data."],"exampleFix":"// before\nconvertArbitraryBase('1A', '0123456789', '01')\n// after\nconvertArbitraryBase('1A', '0123456789ABCDEF', '01')","handlingStrategy":"validation","validationCode":"const srcSet = new Set([...srcAlpha])\nconst allValid = [...input].every((ch) => srcSet.has(ch))\nif (!allValid) {\n  throw new Error('Input contains characters outside the source alphabet')\n}\nconvertArbitraryBase(input, srcAlpha, dstAlpha)","typeGuard":"const inputWithinAlphabet = (input, alpha) =>\n  [...input].every((ch) => alpha.includes(ch))","tryCatchPattern":"try {\n  convertArbitraryBase(input, srcAlpha, dstAlpha)\n} catch (e) {\n  if (/Not a valid character/.test(e.message)) {\n    const bad = e.message.replace('Not a valid character: ', '')\n    // log/strip/normalize the offending glyph, then retry or report\n  }\n  throw e\n}","preventionTips":["Trim and normalize case on input to match the alphabet.","Validate each input glyph is in the alphabet before calling.","Inspect caught ${digit} values to find hidden whitespace or combining marks."],"tags":["validation","base-conversion","charset","out-of-range","conversions"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}