{"record":{"id":"7a25cc6898322618","repo":"TheAlgorithms/JavaScript","slug":"arguments-must-be-numbers","errorCode":null,"errorMessage":"Arguments must be numbers","messagePattern":"Arguments must be numbers","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Maths/GetEuclidGCD.js","lineNumber":3,"sourceCode":"function CheckInput(a, b) {\n  if (typeof a !== 'number' || typeof b !== 'number') {\n    throw new TypeError('Arguments must be numbers')\n  }\n}\n\n/**\n * GetEuclidGCD Euclidean algorithm to determine the GCD of two numbers\n * @param {Number} a integer (may be negative)\n * @param {Number} b integer (may be negative)\n * @returns {Number} Greatest Common Divisor gcd(a, b)\n */\nexport function GetEuclidGCD(a, b) {\n  CheckInput(a, b)\n  a = Math.abs(a)\n  b = Math.abs(b)\n  while (b !== 0) {\n    const rem = a % b\n    a = b\n    b = rem\n  }","sourceCodeStart":1,"sourceCodeEnd":21,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/GetEuclidGCD.js#L1-L21","documentation":"Thrown by the internal CheckInput(a, b) helper (GetEuclidGCD.js:1) as a TypeError when either a or b is not of type 'number'. GetEuclidGCD calls CheckInput at the start of every invocation before applying Math.abs and running the Euclidean algorithm. Note the helper only checks type, not finiteness or integer-ness — NaN is typeof 'number' and passes, and floats are accepted (the algorithm will still run). The GCD of NaN inputs is NaN.","triggerScenarios":"Call GetEuclidGCD('12', 8) with a string; GetEuclidGCD(undefined, 8) from a missing argument; GetEuclidGCD(null, 8) where null is typeof 'object'; GetEuclidGCD([12], 8) passing an array.","commonSituations":"Form or CLI inputs parsed as strings; optional parameters that arrived undefined; values pulled from JSON where numbers were quoted; BigInt inputs (typeof 'bigint').","solutions":["Coerce both arguments with Number() and verify with Number.isFinite before calling — the helper does not reject NaN.","Add a stronger guard than the library's own check if you need to exclude NaN, Infinity, or non-integers.","Catch TypeError at the boundary since this function throws TypeError.","Ensure both arguments are supplied — there are no defaults."],"exampleFix":"// before\nconst g = GetEuclidGCD(a, b) // a or b may be a string or undefined\n\n// after\nconst [x, y] = [a, b].map(Number)\nif (![x, y].every(Number.isFinite)) throw new TypeError('expected finite numbers')\nconst g = GetEuclidGCD(x, y)","handlingStrategy":"type-guard","validationCode":"const [x, y] = [a, b].map(Number)\nif (![x, y].every(v => Number.isFinite(v))) {\n  throw new TypeError('both arguments must be finite numbers')\n}\nconst g = GetEuclidGCD(x, y)","typeGuard":"const areFiniteNumbers = (...vals) => vals.every(v => typeof v === 'number' && Number.isFinite(v))","tryCatchPattern":"try {\n  g = GetEuclidGCD(a, b)\n} catch (e) {\n  if (e instanceof TypeError && e.message === 'Arguments must be numbers') {\n    // non-number input — coerce with Number() and retry\n  } else throw e\n}","preventionTips":["The library's CheckInput only tests typeof — also guard NaN/Infinity yourself.","Both arguments are required; there are no defaults.","Coerce string inputs at the boundary, since JSON often quotes numbers."],"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"}