{"record":{"id":"7f2cb838e4c2fd04","repo":"TheAlgorithms/JavaScript","slug":"argument-is-not-a-number-7f2cb8","errorCode":null,"errorMessage":"Argument is not a number.","messagePattern":"Argument is not a number\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Maths/CheckKishnamurthyNumber.js","lineNumber":27,"sourceCode":"const factorial = (n) => {\n  let fact = 1\n  while (n !== 0) {\n    fact = fact * n\n    n--\n  }\n  return fact\n}\n\n/**\n * krishnamurthy number is a number the sum of the factorial of the all dights is equal to the number itself.\n * @param {Number} number a number for checking is krishnamurthy number or not.\n * @returns return correspond boolean value, if the number is krishnamurthy number return `true` else return `false`.\n * @example 145 => 1! + 4! + 5! = 1  + 24 + 120 = 145\n */\nconst CheckKishnamurthyNumber = (number) => {\n  // firstly, check that input is a number or not.\n  if (typeof number !== 'number') {\n    throw new TypeError('Argument is not a number.')\n  }\n  if (number === 0) {\n    return false\n  }\n  // create a variable to store the sum of all digits factorial.\n  let sumOfAllDigitFactorial = 0\n  // convert the number to string for convenience.\n  let newNumber = number\n  // Extract number digits using the remainder method.\n  while (newNumber > 0) {\n    const lastDigit = newNumber % 10\n    // calculate each digit factorial.\n    sumOfAllDigitFactorial += factorial(lastDigit)\n    newNumber = Math.floor(newNumber / 10)\n  }\n  // if the sumOfAllDigitFactorial is equal to the given number it means the number is a Krishnamurthy number.\n  return sumOfAllDigitFactorial === number\n}","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/CheckKishnamurthyNumber.js#L9-L45","documentation":"Thrown by CheckKishnamurthyNumber(number) (CheckKishnamurthyNumber.js:26) as a TypeError when the argument is not of type 'number'. A Krishnamurthy (strong) number equals the sum of the factorials of its digits (e.g. 145 = 1!+4!+5!); the digit-extraction loop uses modulo and comparison operators that misbehave on non-numbers. The function special-cases number === 0 returning false before entering the loop.","triggerScenarios":"Call CheckKishnamurthyNumber('145') with a string; CheckKishnamurthyNumber(undefined) from a missing param; CheckKishnamurthyNumber([145]) passing an array; CheckKishnamurthyNumber(BigInt(145)).","commonSituations":"User or file input read as a string and not coerced; destructuring that yielded undefined; values from a loosely-typed API; BigInt introduced for large inputs (typeof 'bigint' not 'number').","solutions":["Coerce with Number() and verify with Number.isFinite before calling.","Add a type guard at the call boundary.","Catch TypeError specifically since this function does throw TypeError (unlike some sibling modules).","For string inputs, also strip whitespace and validate format before Number()."],"exampleFix":"// before\nconst r = CheckKishnamurthyNumber(input) // input may be a string\n\n// after\nconst n = Number(input)\nif (!Number.isFinite(n)) throw new TypeError('expected a finite number')\nconst r = CheckKishnamurthyNumber(n)","handlingStrategy":"type-guard","validationCode":"const n = Number(input)\nif (!Number.isFinite(n)) {\n  throw new TypeError('expected a finite number')\n}\nconst result = CheckKishnamurthyNumber(n)","typeGuard":"const isFiniteNumber = (v) => typeof v === 'number' && Number.isFinite(v)","tryCatchPattern":"try {\n  result = CheckKishnamurthyNumber(n)\n} catch (e) {\n  if (e instanceof TypeError && /not a number/i.test(e.message)) {\n    // non-number input — coerce with Number() and retry\n  } else throw e\n}","preventionTips":["This function throws TypeError (unlike some sibling modules that throw plain Error).","BigInt is not supported; convert with Number() within the safe integer range.","Coerce all external string input at the system boundary, not at individual call sites."],"tags":["type-check","input-validation","numeric","krishnamurthy"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}