{"record":{"id":"41549ce482af79b2","repo":"TheAlgorithms/JavaScript","slug":"modulus-must-be-initialized-in-the-object-construc","errorCode":null,"errorMessage":"Modulus must be initialized in the object constructor","messagePattern":"Modulus must be initialized in the object constructor","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Maths/ModularArithmetic.js","lineNumber":17,"sourceCode":"import { extendedEuclideanGCD } from './ExtendedEuclideanGCD'\n\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","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/ModularArithmetic.js#L1-L35","documentation":"ModRing is a class for modular arithmetic operations. The constructor stores MOD, and isInputValid checks that MOD is truthy before performing arithmetic. Note: the check `!this.MOD` also rejects MOD=0, which is mathematically correct (modulus 0 is undefined) but the error message emphasizes initialization rather than the zero case.","triggerScenarios":"Calling new ModRing() with no argument (MOD is undefined), new ModRing(0), new ModRing(null), then invoking any method like add, subtract, etc.","commonSituations":"Constructor called without arguments, MOD read from a config file that is missing or empty, or MOD=0 from a failed parseInt.","solutions":["Pass a positive integer modulus to the ModRing constructor.","Ensure MOD is defined and > 0 before constructing the ring.","Validate the config value for modulus before creating the ModRing instance."],"exampleFix":"// before\nconst ring = new ModRing()\nring.add(3, 5)\n// after\nconst ring = new ModRing(7)\nring.add(3, 5)","handlingStrategy":"validation","validationCode":"if (typeof modulus !== 'number' || modulus <= 0) {\n  throw new RangeError('modulus must be a positive number')\n}\nconst ring = new ModRing(modulus)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass a positive integer modulus to the ModRing constructor.","Validate config values for modulus before constructing the ring instance.","Remember that MOD=0 is rejected; modulus must be strictly positive."],"tags":["math","object-state","precondition","modular-arithmetic"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}