{"record":{"id":"6df46bb38b50777f","repo":"TheAlgorithms/JavaScript","slug":"arguments-type-are-invalid","errorCode":null,"errorMessage":"Arguments type are invalid","messagePattern":"Arguments type are invalid","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Ciphers/XORCipher.js","lineNumber":14,"sourceCode":"/**\n * @function XORCipher\n * @description - Encrypt using an XOR cipher\n * The XOR cipher is a type of additive cipher.\n * Each character is bitwise XORed with the key.\n * We loop through the input string, XORing each\n * character with the key.\n * @param {string} str - string to be encrypted\n * @param {number} key - key for encryption\n * @return {string} encrypted string\n */\nconst XORCipher = (str, key) => {\n  if (typeof str !== 'string' || !Number.isInteger(key)) {\n    throw new TypeError('Arguments type are invalid')\n  }\n\n  return str.replace(/./g, (char) =>\n    String.fromCharCode(char.charCodeAt() ^ key)\n  )\n}\n\nexport default XORCipher\n","sourceCodeStart":1,"sourceCodeEnd":23,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Ciphers/XORCipher.js#L1-L23","documentation":"XORCipher rejects the call when str is not a string OR key is not an integer. It XORs each character's charCodeAt with the key bitwise, which only makes sense for a string input and a whole-number key; a float key or non-string str would yield undefined/garbled output.","triggerScenarios":"str is a number/object/Buffer, or key is a float (e.g. 3.5), a string (\"7\"), NaN, undefined, or any non-integer.","commonSituations":"Key read from a config/env as a string, str passed as a Buffer from file/crypto input, or a computed key that ended up fractional.","solutions":["Pass a string and an integer key: XORCipher('msg', 42).","Parse the key: Math.trunc(Number(rawKey)).","Decode str buffers to utf8 first."],"exampleFix":"// before\nXORCipher(buffer, process.env.XOR_KEY) // Buffer + string\n// after\nXORCipher(buffer.toString('utf8'), Math.trunc(Number(process.env.XOR_KEY)))","handlingStrategy":"validation","validationCode":"function xorSafe(text, rawKey) {\n  const key = Math.trunc(Number(rawKey));\n  return XORCipher(String(text), key);\n}","typeGuard":"/** @param {unknown} s @param {unknown} k @returns {boolean} */\nconst validXorArgs = (s, k) => typeof s === 'string' && Number.isInteger(k);","tryCatchPattern":"try { return XORCipher(text, key); }\ncatch (e) {\n  if (e instanceof TypeError && /Arguments type are invalid/.test(e.message)) {\n    return XORCipher(String(text), Math.trunc(Number(key)));\n  }\n  throw e;\n}","preventionTips":["Parse the key with Math.trunc(Number(...)) at the boundary.","Decode str Buffers to utf8 before ciphering.","Validate str is a string and key is an integer separately for clarity."],"tags":["cipher","xor","type-validation","integer"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}