{"record":{"id":"09f120e0e8532f5f","repo":"TheAlgorithms/JavaScript","slug":"type-of-n-must-be-number","errorCode":null,"errorMessage":"Type of n must be number","messagePattern":"Type of n must be number","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Maths/AutomorphicNumber.js","lineNumber":20,"sourceCode":" * @function isAutomorphic\n * @author [SilverDragonOfR] (https://github.com/SilverDragonOfR)\n *\n * @see [Automorphic] (https://en.wikipedia.org/wiki/Automorphic_number)\n * @description This script will check whether a number is Automorphic or not\n * @description A number n is said to be a Automorphic number if the square of n ends in the same digits as n itself.\n *\n * @param {Integer} n - the n for nth Catalan Number\n * @return {Integer} - the nth Catalan Number\n * @complexity Time: O(log10(n)) , Space: O(1)\n *\n * @convention We define Automorphic only for whole number integers. For negetive integer we return False. For float or String we show error.\n * @examples 0, 1, 5, 6, 25, 76, 376, 625, 9376 are some Automorphic numbers\n */\n\n// n is the number to be checked\nexport const isAutomorphic = (n) => {\n  if (typeof n !== 'number') {\n    throw new Error('Type of n must be number')\n  }\n  if (!Number.isInteger(n)) {\n    throw new Error('n cannot be a floating point number')\n  }\n  if (n < 0) {\n    return false\n  }\n\n  // now n is a whole number integer >= 0\n  let n_sq = n * n\n  while (n > 0) {\n    if (n % 10 !== n_sq % 10) {\n      return false\n    }\n    n = Math.floor(n / 10)\n    n_sq = Math.floor(n_sq / 10)\n  }\n","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/AutomorphicNumber.js#L2-L38","documentation":"Thrown by isAutomorphic(n) (AutomorphicNumber.js:19) as a plain Error when n is not of type 'number'. An automorphic number is one whose square ends in the number itself (e.g. 25 -> 625), and the digit-extraction algorithm relies on arithmetic operators that behave incorrectly on non-numbers. Note the library throws plain Error rather than TypeError despite this being a type check, and note the doc-comment convention: negatives return false, but non-numbers and floats throw.","triggerScenarios":"Call isAutomorphic('25') with a string; isAutomorphic(null) or isAutomorphic(undefined) from an unguarded optional field; isAutomorphic(BigInt(25)) which is typeof 'bigint' not 'number'.","commonSituations":"Parsing input with parseInt/Number but forgetting to handle NaN; values pulled from JSON where the field was a string; BigInt usage introduced for large numbers (BigInt is not typeof 'number').","solutions":["Convert the input with Number() and verify !Number.isNaN(n) before calling.","If you use BigInt for large automorphic checks, note this function does not support it; convert with Number() only if value is within safe integer range.","Wrap the call and catch plain Error (the library does not throw TypeError here).","Validate at the API boundary with a type guard before passing through."],"exampleFix":"// before\nconst result = isAutomorphic(userInput) // userInput may be a string\n\n// after\nconst n = Number(userInput)\nif (!Number.isFinite(n)) throw new TypeError('expected a finite number')\nconst result = isAutomorphic(n)","handlingStrategy":"type-guard","validationCode":"const n = Number(input)\nif (!Number.isFinite(n)) {\n  throw new TypeError('expected a finite number')\n}\nconst result = isAutomorphic(n)","typeGuard":"const isFiniteNumber = (v) => typeof v === 'number' && Number.isFinite(v)","tryCatchPattern":"try {\n  result = isAutomorphic(n)\n} catch (e) {\n  if (e instanceof Error && e.message === 'Type of n must be number') {\n    // non-number input — coerce and retry with Number()\n  } else throw e\n}","preventionTips":["Remember this module throws plain Error, not TypeError, for the type check.","BigInt is not accepted — convert with Number() only when within safe range.","Validate at the boundary where external (string) data enters your system."],"tags":["type-check","input-validation","numeric","automorphic"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}