{"record":{"id":"f8b3cdd2a71caf9d","repo":"TheAlgorithms/JavaScript","slug":"coefficient-a-b-should-be-number","errorCode":null,"errorMessage":"Coefficient a, b should be number","messagePattern":"Coefficient a, b should be number","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Ciphers/AffineCipher.js","lineNumber":41,"sourceCode":" * @param {Number} m - Modulos value\n * @return {Number} Return modular multiplicative inverse of coefficient a and modulos m\n */\nfunction inverseMod(a, m) {\n  for (let x = 1; x < m; x++) {\n    if (mod(a * x, m) === 1) return x\n  }\n}\n\n/**\n * Argument validation\n * @param {String} str - String to be checked\n * @param {Number} a - A coefficient to be checked\n * @param {Number} b - B coefficient to be checked\n * @return {Boolean} Result of the checking\n */\nfunction isCorrectFormat(str, a, b) {\n  if (typeof a !== 'number' || typeof b !== 'number') {\n    throw new TypeError('Coefficient a, b should be number')\n  }\n\n  if (typeof str !== 'string') {\n    throw new TypeError('Argument str should be String')\n  }\n\n  if (!CoPrimeCheck(a, 26)) {\n    throw new Error(a + ' is not coprime of 26')\n  }\n\n  return true\n}\n\n/**\n * Find character index based on ASCII order\n * @param {String} char - Character index to be found\n * @return {Boolean} Character index\n */","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Ciphers/AffineCipher.js#L23-L59","documentation":"Thrown by isCorrectFormat() in the Affine cipher when coefficient a or b is not of type 'number'. Affine encryption computes (a*x + b) mod 26, which requires numeric arithmetic; passing a string/undefined/null would yield NaN ciphertext silently, so it is rejected.","triggerScenarios":"Passing a = \"5\" (string), b = undefined, either coefficient omitted, or values pulled from inputs without conversion.","commonSituations":"Form input values (strings), destructuring that left a coefficient undefined, or a default-argument refactor that dropped a value.","solutions":["Pass numeric literals: encrypt(text, 5, 8).","Convert string sources with Number()/parseInt before calling.","Destructure with explicit numeric defaults."],"exampleFix":"// before\nencrypt(text, formData.a, formData.b) // strings\n// after\nencrypt(text, Number(formData.a), Number(formData.b))","handlingStrategy":"type-guard","validationCode":"function affineSafe(text, a, b) {\n  return encrypt(text, Number(a), Number(b));\n}","typeGuard":"/** @param {unknown} x @returns {x is number} */\nconst isNum = x => typeof x === 'number' && !Number.isNaN(x);","tryCatchPattern":"try { return encrypt(text, a, b); }\ncatch (e) {\n  if (e instanceof TypeError && /Coefficient/.test(e.message)) {\n    return encrypt(text, Number(a), Number(b));\n  }\n  throw e;\n}","preventionTips":["Convert string coefficients with Number() at the boundary.","Provide numeric defaults when destructuring options.","Validate typeof === 'number' before encrypting."],"tags":["cipher","affine","type-validation","coefficient"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}