{"record":{"id":"98d7f9471529cbad","repo":"TheAlgorithms/JavaScript","slug":"not-a-number","errorCode":null,"errorMessage":"Not a Number","messagePattern":"Not a Number","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Maths/ExtendedEuclideanGCD.js","lineNumber":29,"sourceCode":" * This is called Bézout's identity and the coefficients are called Bézout coefficients\n *\n * The algorithm uses the Euclidean method of getting remainder:\n * r_i+1 = r_i-1 - qi*ri\n * and applies it to series s and t (with same quotient q at each stage)\n * When r_n reaches 0, the value r_n-1 gives the gcd, and s_n-1 and t_n-1 give the coefficients\n *\n * This implementation uses an iterative approach to calculate the values\n */\n\n/**\n *\n * @param {Number} arg1 first argument\n * @param {Number} arg2 second argument\n * @returns Array with GCD and first and second Bézout coefficients\n */\nconst extendedEuclideanGCD = (arg1, arg2) => {\n  if (typeof arg1 !== 'number' || typeof arg2 !== 'number')\n    throw new TypeError('Not a Number')\n  if (arg1 < 1 || arg2 < 1) throw new TypeError('Must be positive numbers')\n\n  // Make the order of coefficients correct, as the algorithm assumes r0 > r1\n  if (arg1 < arg2) {\n    const res = extendedEuclideanGCD(arg2, arg1)\n    const temp = res[1]\n    res[1] = res[2]\n    res[2] = temp\n    return res\n  }\n\n  // At this point arg1 > arg2\n\n  // Remainder values\n  let r0 = arg1\n  let r1 = arg2\n\n  // Coefficient1 values","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/ExtendedEuclideanGCD.js#L11-L47","documentation":"Thrown by extendedEuclideanGCD(arg1, arg2) (ExtendedEuclideanGCD.js:28) as a TypeError when either argument is not of type 'number'. The function computes the GCD along with the Bezout coefficients using an iterative algorithm that assumes numeric operands. A second check immediately after rejects values less than 1 with 'Must be positive numbers', so this specific error fires only on non-number inputs (including BigInt, which is typeof 'bigint').","triggerScenarios":"Call extendedEuclideanGCD('12', 8) with a string; extendedEuclideanGCD(undefined, 8) from a missing argument; extendedEuclideanGCD(BigInt(12), BigInt(8)); extendedEuclideanGCD(null, null) from a JSON null.","commonSituations":"Command-line or form inputs parsed as strings; BigInt used to avoid precision loss on large numbers (this function does not support BigInt); optional params that arrived undefined; deserialized JSON with stringified numbers.","solutions":["Coerce both arguments with Number() and verify Number.isFinite.","If you need large-integer GCD, use a BigInt-native implementation instead.","Catch TypeError at the boundary since this function does throw TypeError.","Validate both args are positive after the type check, since the next guard rejects < 1."],"exampleFix":"// before\nconst r = extendedEuclideanGCD(a, b) // a or b may be a string\n\n// after\nconst [x, y] = [a, b].map(Number)\nif (![x, y].every(Number.isFinite)) throw new TypeError('expected finite numbers')\nconst r = extendedEuclideanGCD(x, y)","handlingStrategy":"type-guard","validationCode":"const [x, y] = [arg1, arg2].map(Number)\nif (![x, y].every(v => Number.isFinite(v) && v >= 1)) {\n  throw new TypeError('both arguments must be finite numbers >= 1')\n}\nconst r = extendedEuclideanGCD(x, y)","typeGuard":"const isPositiveNumber = (v) => typeof v === 'number' && Number.isFinite(v) && v >= 1","tryCatchPattern":"try {\n  r = extendedEuclideanGCD(a, b)\n} catch (e) {\n  if (e instanceof TypeError && e.message === 'Not a Number') {\n    // non-number input — coerce and retry\n  } else throw e\n}","preventionTips":["This function requires both args >= 1; a second guard rejects values below 1.","BigInt is not supported — use a BigInt-native GCD for large integers.","Coerce string inputs with Number() and verify finiteness before calling."],"tags":["type-check","input-validation","numeric","gcd","number-theory"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}