{"record":{"id":"b3810418f939316e","repo":"TheAlgorithms/JavaScript","slug":"input-data-must-be-numbers","errorCode":null,"errorMessage":"Input data must be numbers","messagePattern":"Input data must be numbers","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Maths/IntToBase.js","lineNumber":17,"sourceCode":"/**\n * @function intToBase\n * @description Convert a number from decimal system to another (till decimal)\n * @param {Number} number Number to be converted\n * @param {Number} base Base of new number system\n * @returns {String} Converted Number\n * @see [HornerMethod](https://en.wikipedia.org/wiki/Horner%27s_method)\n * @example\n * const num1 = 125 // Needs to be converted to the binary number system\n * gornerScheme(num, 2); // ===> 1111101\n * @example\n * const num2 = 125 // Needs to be converted to the octal number system\n * gornerScheme(num, 8); // ===> 175\n */\nconst intToBase = (number, base) => {\n  if (typeof number !== 'number' || typeof base !== 'number') {\n    throw new Error('Input data must be numbers')\n  }\n  // Zero in any number system is zero\n  if (number === 0) {\n    return '0'\n  }\n  let absoluteValue = Math.abs(number)\n  let convertedNumber = ''\n  while (absoluteValue > 0) {\n    // Every iteration last digit is taken away\n    // and added to the previous one\n    const lastDigit = absoluteValue % base\n    convertedNumber = lastDigit + convertedNumber\n    absoluteValue = Math.trunc(absoluteValue / base)\n  }\n  // Result is whether negative or positive,\n  // depending on the original value\n  if (number < 0) {\n    convertedNumber = '-' + convertedNumber","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/IntToBase.js#L1-L35","documentation":"The intToBase function converts an integer to its string representation in a given base using Horner's method (repeated modulo and division). Both parameters must be JavaScript number primitives; strings or other types are rejected because the arithmetic operations would produce incorrect results via silent type coercion.","triggerScenarios":"Calling intToBase(\"125\", 2), intToBase(125, \"2\"), or passing any non-number type for either argument. Values from HTML input fields or parsed JSON strings are the most common culprits.","commonSituations":"Values read from HTML form inputs (always strings), JSON where numbers were serialized as quoted strings, CLI arguments (always strings), or config values loaded as strings.","solutions":["Ensure both arguments are number primitives (typeof === 'number').","If reading from user input, parse with Number() or parseInt() before calling.","Wrap the call in a type-check helper that converts or rejects non-number inputs."],"exampleFix":"// before\nintToBase(inputEl.value, 2) // inputEl.value is a string\n// after\nintToBase(Number(inputEl.value), 2)","handlingStrategy":"type-guard","validationCode":"if (typeof number !== 'number' || typeof base !== 'number') {\n  throw new TypeError('Both arguments must be numbers')\n}\nintToBase(number, base)","typeGuard":"const areNumbers = (n, b) => typeof n === 'number' && typeof b === 'number'","tryCatchPattern":null,"preventionTips":["Parse string inputs with Number() or parseInt() before calling math functions.","Remember HTML input field values, JSON-quoted numbers, and CLI args are always strings.","Add type guards at API boundaries where external data enters your code."],"tags":["math","type-check","input-validation","base-conversion"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}