{"record":{"id":"0fa75deac06c570f","repo":"TheAlgorithms/JavaScript","slug":"argument-str-should-be-string","errorCode":null,"errorMessage":"Argument str should be String","messagePattern":"Argument str should be String","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Ciphers/AffineCipher.js","lineNumber":45,"sourceCode":"  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 */\nfunction findCharIndex(char) {\n  return char.toUpperCase().charCodeAt(0) - 'A'.charCodeAt(0)\n}\n","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Ciphers/AffineCipher.js#L27-L63","documentation":"Thrown by isCorrectFormat() when the plaintext str is not a string. Affine cipher operates character-by-character via charCodeAt, so a non-string would either coerce incorrectly or throw deeper; the guard fails fast with a clear message.","triggerScenarios":"Passing a number, object, array, null, undefined, or Buffer where the plaintext string is expected.","commonSituations":"Passing a Buffer/Uint8Array from a file read instead of decoded text, or forgetting to JSON.stringify an object before encrypting.","solutions":["Pass a real string: encrypt('hello', a, b).","Decode buffers: Buffer.from(...).toString('utf8').","Stringify objects: JSON.stringify(obj) if that is the intent."],"exampleFix":"// before\nencrypt(buffer, a, b) // Buffer\n// after\nencrypt(buffer.toString('utf8'), a, b)","handlingStrategy":"type-guard","validationCode":"function ensureString(v) {\n  return typeof v === 'string' ? v : String(v);\n}\n// encrypt(ensureString(text), a, b)","typeGuard":"/** @param {unknown} s @returns {s is string} */\nconst isString = s => typeof s === 'string';","tryCatchPattern":"try { return encrypt(text, a, b); }\ncatch (e) {\n  if (e instanceof TypeError && /str should be String/.test(e.message)) {\n    return encrypt(String(text), a, b);\n  }\n  throw e;\n}","preventionTips":["Decode Buffers/typed arrays to utf8 strings before encrypting.","Stringify objects explicitly rather than relying on coercion.","Validate typeof text === 'string' at the call site."],"tags":["cipher","affine","type-validation","string"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}