{"record":{"id":"9fd79df02fcc22dc","repo":"TheAlgorithms/JavaScript","slug":"unsupported-base-must-be-in-range-2-10","errorCode":null,"errorMessage":"Unsupported base. Must be in range [2, 10]","messagePattern":"Unsupported base\\. Must be in range \\[2, 10\\]","errorType":"exception","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"Maths/DecimalExpansion.js","lineNumber":25,"sourceCode":" * Because this function is recursive, it may throw an error when reaching the\n * maximum call stack size.\n *\n * Returns an array containing : [\n *  0: integer part of the division\n *  1: array of decimals (if any, or an empty array)\n *  2: indexOf 1st cycle digit in decimals array if a/b is periodic, or undef.\n * ]\n *\n * @see https://mathworld.wolfram.com/DecimalExpansion.html\n *\n * @param {number} a\n * @param {number} b\n * @param {number} [base=10]\n * @returns {array}\n */\nexport function decExp(a, b, base = 10, exp = [], d = {}, dlen = 0) {\n  if (base < 2 || base > 10) {\n    throw new RangeError('Unsupported base. Must be in range [2, 10]')\n  }\n\n  if (a === 0) {\n    return [0, [], undefined]\n  }\n\n  if (a === b && dlen === 0) {\n    return [1, [], undefined]\n  }\n\n  // d contains the dividends used so far and the corresponding index of its\n  // euclidean division by b in the expansion array.\n  d[a] = dlen++\n\n  if (a < b) {\n    exp.push(0)\n    return decExp(a * base, b, base, exp, d, dlen)\n  }","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/DecimalExpansion.js#L7-L43","documentation":"Thrown by decExp(a, b, base = 10, ...) (DecimalExpansion.js:24) as a RangeError when the base argument is less than 2 or greater than 10. The function computes the expansion of a/b in the given base using Euclidean division and digit conversion via Number.prototype.toString(base), which itself only supports radix 2-36, but this library further restricts to single-digit bases (2-10). The check runs on every recursive call but base never changes during recursion.","triggerScenarios":"Call decExp(1, 3, 16) requesting hexadecimal (base 16); decExp(1, 3, 0) or decExp(1, 3, 1) with an out-of-range low base; omit the third positional argument incorrectly and pass a value into the base slot that was meant for another parameter.","commonSituations":"Assuming the function supports hexadecimal or other radix > 10 (it does not); confusing the parameter order (the function has internal params exp, d, dlen after base); passing a base from user input without clamping.","solutions":["Restrict base to the supported range before calling: base = Math.min(10, Math.max(2, base)).","If you need base > 10, use a different library or implement the expansion yourself with a custom digit alphabet.","Remember the default base is 10; only pass the third argument when you actually need binary/octal/etc.","Do not pass values into the third slot unless you intend to set the base — the trailing params are internal."],"exampleFix":"// before\nconst e = decExp(1, 3, 16) // throws RangeError\n\n// after\nconst e = decExp(1, 3, 10) // use base 10, or clamp:\n// const base = Math.min(10, Math.max(2, requestedBase))\n// const e = decExp(1, 3, base)","handlingStrategy":"validation","validationCode":"const safeBase = (base == null) ? 10 : Math.min(10, Math.max(2, Math.floor(base)))\nconst e = decExp(a, b, safeBase)","typeGuard":"const isValidBase = (v) => typeof v === 'number' && v >= 2 && v <= 10 && Number.isInteger(v)","tryCatchPattern":"try {\n  e = decExp(a, b, base)\n} catch (e) {\n  if (e instanceof RangeError && /Unsupported base/.test(e.message)) {\n    // base out of [2,10] — clamp and retry\n    e = decExp(a, b, Math.min(10, Math.max(2, base)))\n  } else throw e\n}","preventionTips":["The function only supports bases 2 through 10; do not pass hex (16) or base 1.","Do not pass values into the third slot unless you intend to set the base — trailing params are internal.","Clamp user-supplied base to the supported range at the boundary."],"tags":["range-error","numeric-base","input-validation","radix"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}