{"record":{"id":"13d0f25464f69652","repo":"TheAlgorithms/JavaScript","slug":"input-must-be-numbers","errorCode":null,"errorMessage":"Input must be Numbers","messagePattern":"Input must be Numbers","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Maths/ModularArithmetic.js","lineNumber":20,"sourceCode":"\n/**\n * https://brilliant.org/wiki/modular-arithmetic/\n * @param {Number} arg1 first argument\n * @param {Number} arg2 second argument\n * @returns {Number}\n */\n\nexport class ModRing {\n  constructor(MOD) {\n    this.MOD = MOD\n  }\n\n  isInputValid = (arg1, arg2) => {\n    if (!this.MOD) {\n      throw new Error('Modulus must be initialized in the object constructor')\n    }\n    if (typeof arg1 !== 'number' || typeof arg2 !== 'number') {\n      throw new TypeError('Input must be Numbers')\n    }\n  }\n  /**\n   * Modulus is Distributive property,\n   * As a result, we separate it into numbers in order to keep it within MOD's range\n   */\n\n  add = (arg1, arg2) => {\n    this.isInputValid(arg1, arg2)\n    return ((arg1 % this.MOD) + (arg2 % this.MOD)) % this.MOD\n  }\n\n  subtract = (arg1, arg2) => {\n    this.isInputValid(arg1, arg2)\n    // An extra MOD is added to check negative results\n    return ((arg1 % this.MOD) - (arg2 % this.MOD) + this.MOD) % this.MOD\n  }\n","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/ModularArithmetic.js#L2-L38","documentation":"The ModRing arithmetic methods (add, etc.) call isInputValid which requires both operands to be number primitives. This check fires after the modulus-initialization check. Note that BigInt values fail this check because typeof 3n is 'bigint', not 'number'.","triggerScenarios":"Calling ring.add(\"3\", 5), ring.add(3n, 5) (BigInt), ring.add(null, 5), or passing any non-number type as either argument.","commonSituations":"String numbers from user input, BigInt used for large modular arithmetic, or null/undefined from optional parameters that were not defaulted.","solutions":["Pass number primitives (not strings or BigInts) to ModRing methods.","Convert string input with Number() before calling.","If working with large numbers, avoid BigInt; use plain numbers or a BigInt-compatible library instead."],"exampleFix":"// before\nring.add(a, b) // a or b might be strings\n// after\nring.add(Number(a), Number(b))","handlingStrategy":"type-guard","validationCode":"if (typeof arg1 !== 'number' || typeof arg2 !== 'number') {\n  throw new TypeError('Both arguments must be numbers')\n}\nring.add(arg1, arg2)","typeGuard":"const areNumbers = (a, b) => typeof a === 'number' && typeof b === 'number'","tryCatchPattern":null,"preventionTips":["Avoid using BigInt with ModRing; convert to plain numbers or use a BigInt-compatible library.","Convert string operands to Number() before calling arithmetic methods.","Default optional parameters to 0 rather than leaving them undefined."],"tags":["math","type-check","input-validation","modular-arithmetic"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}