{"record":{"id":"dc5edcb3d955dd32","repo":"TheAlgorithms/JavaScript","slug":"expected-a-number-received-typeof-precision","errorCode":null,"errorMessage":"Expected a number, received ${typeof precision}","messagePattern":"Expected a number, received (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Maths/SquareRoot.js","lineNumber":14,"sourceCode":"/*\n * Author: Rak Laptudirm\n *\n * https://en.wikipedia.org/wiki/Newton%27s_method\n *\n * Finding the square root of a number using Newton's method.\n */\n\nfunction sqrt(num, precision = 4) {\n  if (!Number.isFinite(num)) {\n    throw new TypeError(`Expected a number, received ${typeof num}`)\n  }\n  if (!Number.isFinite(precision)) {\n    throw new TypeError(`Expected a number, received ${typeof precision}`)\n  }\n  let sqrt = 1\n  for (let i = 0; i < precision; i++) {\n    sqrt -= (sqrt * sqrt - num) / (2 * sqrt)\n  }\n  return sqrt\n}\n\nexport { sqrt }\n","sourceCodeStart":1,"sourceCodeEnd":24,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/SquareRoot.js#L1-L24","documentation":"Thrown by the second parameter guard in `sqrt(num, precision = 4)`. Although `precision` has a default of `4`, passing an explicit non-finite value (NaN, Infinity, a string, undefined bypassing the default via `sqrt(9, undefined)`) triggers the throw. The value drives the fixed iteration count `for (let i = 0; i < precision; i++)`, so it must be a finite number.","triggerScenarios":"Call `sqrt(9, undefined)` (defeating the default), `sqrt(9, '4')`, `sqrt(9, NaN)`, `sqrt(9, Infinity)`, or pass a computed precision that came back NaN.","commonSituations":"Forwarding an optional config field that was undefined, accepting a precision from JSON config (string), or treating a '0 precision' edge case where the caller computes `1/0`.","solutions":["Omit the second argument entirely to use the default `precision = 4`.","Coerce with `Number(precision)` and confirm `Number.isFinite` before passing.","When forwarding optional values, fall back: `sqrt(num, precision ?? 4)`."],"exampleFix":"// before\nsqrt(num, config.iterations) // config.iterations may be a string or undefined\n\n// after\nconst p = Number(config.iterations)\nsqrt(num, Number.isFinite(p) ? p : 4)","handlingStrategy":"validation","validationCode":"const p = Number(precision)\nif (!Number.isFinite(p)) {\n  sqrt(num) // fall back to default precision = 4\n} else {\n  sqrt(num, p)\n}","typeGuard":"const isOptionalFiniteNumber = (x) => x === undefined || (typeof x === 'number' && Number.isFinite(x))","tryCatchPattern":null,"preventionTips":["Omit optional parameters rather than passing undefined explicitly.","Use nullish coalescing: `sqrt(num, precision ?? 4)` to fall back to the default.","Coerce config values from JSON (always strings for numbers) before forwarding."],"tags":["type-validation","math","optional-parameter","default-parameter"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}