A number's base (or radix) is how many distinct digits it uses before carrying into the next column: base 10 has ten digits 0-9, binary has two, hexadecimal has sixteen (0-9 then a-f). baseConverter(str, fromBase, toBase) reads the integer written in str using fromBase's digits and rewrites it using toBase's digits, returning a string. Bases run from 2 to 36 — after 9 the digits continue a (value 10) through z (value 35). See radix for background.
baseConverter(
str: string, // the number, written in fromBase
fromBase: number, // 2..36 — the base str is written in
toBase: number, // 2..36 — the base to convert to
): string // the number rewritten in toBase, lowercase
baseConverter('ff', 16, 10); // '255'
baseConverter('255', 10, 16); // 'ff'
baseConverter('1010', 2, 10); // '10'
baseConverter('z', 36, 10); // '35'
baseConverter('-ff', 16, 10); // '-255' (sign preserved)
baseConverter('9', 8, 10); // throws RangeError — 9 is not a base-8 digit
0-9 cover values 0 through 9, then a-z cover 10 through 35. Input is case-insensitive (FF and ff both parse); output is always lowercase.fromBase and toBase must be integers in 2..36. Anything else (base 1, base 37, 2.5) throws a RangeError.str must have a value below fromBase. A stray 9 in a base-8 number, or a g in hex, throws a RangeError. Do not silently stop reading at the first bad digit.- is preserved on the output. The value 0 is always "0", never "-0".Number.MAX_SAFE_INTEGER (2^53 − 1), so plain number arithmetic is enough; arbitrarily large inputs are out of scope.parseInt or Number.prototype.toString to do the conversion.Unlock the solution & editor
Runnable editor + tests
Solve in the browser with instant Jest feedback.
Detailed solutions
Walkthroughs, edge cases, and complexity notes.
Multi-framework variants
React, Vue, Vanilla, Angular — same question, different stacks.