{"record":{"id":"8990d26625e19492","repo":"TheAlgorithms/JavaScript","slug":"input-data-must-be-numbers-8990d2","errorCode":null,"errorMessage":"Input data must be numbers","messagePattern":"Input data must be numbers","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Maths/SquareRootLogarithmic.js","lineNumber":20,"sourceCode":" * @function squareRootLogarithmic\n * @description\n * Return the square root of 'num' rounded down\n * to the nearest integer.\n * More info: https://leetcode.com/problems/sqrtx/\n * @param {Number} num Number whose square of root is to be found\n * @returns {Number} Square root\n * @see [BinarySearch](https://en.wikipedia.org/wiki/Binary_search_algorithm)\n * @example\n * const num1 = 4\n * logarithmicSquareRoot(num1) // ====> 2\n * @example\n * const num2 = 8\n * logarithmicSquareRoot(num1) // ====> 2\n *\n */\nconst squareRootLogarithmic = (num) => {\n  if (typeof num !== 'number') {\n    throw new Error('Input data must be numbers')\n  }\n  let answer = 0\n  let sqrt = 0\n  let edge = num\n\n  while (sqrt <= edge) {\n    const mid = Math.trunc((sqrt + edge) / 2)\n    if (mid * mid === num) {\n      return mid\n    } else if (mid * mid < num) {\n      sqrt = mid + 1\n      answer = mid\n    } else {\n      edge = mid - 1\n    }\n  }\n\n  return answer","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/SquareRootLogarithmic.js#L2-L38","documentation":"Thrown by `squareRootLogarithmic(num)` when `typeof num !== 'number'`. The function uses binary search between 0 and `num`, so a non-number would break the loop arithmetic. Note the guard only checks `typeof`, so NaN (which is technically `typeof === 'number'`) slips through and yields an incorrect answer rather than this error.","triggerScenarios":"Call `squareRootLogarithmic('16')`, `squareRootLogarithmic(null)`, `squareRootLogarithmic(undefined)`, `squareRootLogarithmic(BigInt(16))`. Passing NaN does NOT throw (it returns NaN).","commonSituations":"JSON-parsed input where numbers arrive as strings, BigInt arithmetic mixed with Number, or unguarded destructuring from an object whose field was absent.","solutions":["Coerce with `Number(num)` and reject NaN explicitly before calling.","If your input is a string, `parseFloat` and validate the result is finite.","Add a `Number.isFinite` check upstream so NaN never reaches this function."],"exampleFix":"// before\nsquareRootLogarithmic(payload.value) // payload.value may be '16'\n\n// after\nconst v = Number(payload.value)\nif (!Number.isFinite(v)) throw new TypeError('value must be a finite number')\nsquareRootLogarithmic(v)","handlingStrategy":"type-guard","validationCode":"const v = Number(num)\nif (!Number.isFinite(v)) {\n  throw new TypeError('num must be a finite number')\n}\nsquareRootLogarithmic(v)","typeGuard":"const isFiniteNumber = (x) => typeof x === 'number' && Number.isFinite(x)","tryCatchPattern":null,"preventionTips":["Do not rely on typeof alone - it lets NaN through.","Validate with Number.isFinite at the boundary, not at the call site.","Reject BigInt explicitly if your pipeline may produce one."],"tags":["type-validation","math","binary-search","nan-pitfall"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}