{"record":{"id":"9a047561a56046aa","repo":"TheAlgorithms/JavaScript","slug":"argument-not-an-integer","errorCode":null,"errorMessage":"Argument not an Integer","messagePattern":"Argument not an Integer","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Bit-Manipulation/BinaryCountSetBits.js","lineNumber":15,"sourceCode":"/*\n    author: vivek9patel\n    license: GPL-3.0 or later\n\n    This script will find number of 1's\n    in binary representation of given number\n\n*/\n\nfunction BinaryCountSetBits(a) {\n  'use strict'\n\n  // check whether input is an integer, some non-integer number like, 21.1 have non-terminating binary expansions and hence their binary expansion will contain infinite ones, thus the handling of non-integers (including strings,objects etc. as it is meaningless) has been omitted\n\n  if (!Number.isInteger(a)) throw new TypeError('Argument not an Integer')\n\n  let count = 0\n  while (a) {\n    a &= a - 1\n    count++\n  }\n\n  return count\n}\n\nexport { BinaryCountSetBits }\n","sourceCodeStart":1,"sourceCodeEnd":27,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Bit-Manipulation/BinaryCountSetBits.js#L1-L27","documentation":"BinaryCountSetBits counts set bits via Brian Kernighan's algorithm (a &= a - 1). Non-integer numbers like 21.1 have non-terminating binary expansions, which would loop infinitely counting 1-bits, so the function rejects anything that fails Number.isInteger.","triggerScenarios":"Passing a float (21.1), a numeric string (\"5\"), NaN, Infinity, undefined, null, or any non-number. Note: negative integers work but loop on two's-complement bits.","commonSituations":"Receiving input from a form/input element (always a string), a parseFloat result, or a division that yielded a fraction.","solutions":["Pass a plain integer, e.g. BinaryCountSetBits(13).","Coerce strings/fractions explicitly first: BinaryCountSetBits(Math.trunc(Number(input))).","Guard with Number.isInteger(x) before calling if the source is untrusted."],"exampleFix":"// before\nBinaryCountSetBits(formData.age) // string from input\n// after\nconst n = Math.trunc(Number(formData.age))\nif (Number.isInteger(n)) BinaryCountSetBits(n)","handlingStrategy":"type-guard","validationCode":"function countBitsSafe(x) {\n  const n = Math.trunc(Number(x));\n  if (!Number.isInteger(n)) throw new TypeError('Argument not an Integer');\n  return BinaryCountSetBits(n);\n}","typeGuard":"/** @param {unknown} x @returns {x is number} */\nconst isInt = x => typeof x === 'number' && Number.isInteger(x);","tryCatchPattern":"try { return BinaryCountSetBits(value); }\ncatch (e) {\n  if (e instanceof TypeError && /not an Integer/.test(e.message)) {\n    return BinaryCountSetBits(Math.trunc(Number(value)));\n  }\n  throw e;\n}","preventionTips":["Convert form/input strings with Number() and Math.trunc before calling.","Validate with Number.isInteger at API boundaries.","Remember negative integers are accepted and count two's-complement bits."],"tags":["bit-manipulation","type-validation","integer"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}