{"record":{"id":"640162b7292d4354","repo":"TheAlgorithms/JavaScript","slug":"arguments-are-invalid","errorCode":null,"errorMessage":"Arguments are invalid","messagePattern":"Arguments are invalid","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Ciphers/CaesarCipher.js","lineNumber":11,"sourceCode":"/**\n * @function caesarsCipher\n * @description - In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on. The method is named after Julius Caesar, who used it in his private correspondence.\n * @see - [wiki](https://en.wikipedia.org/wiki/Caesar_cipher)\n * @param  {string} str - string to be encrypted\n * @param {number} rotation - the number of rotation, expect real number ( > 0)\n * @return {string} - decrypted string\n */\nconst caesarCipher = (str, rotation) => {\n  if (typeof str !== 'string' || !Number.isInteger(rotation) || rotation < 0) {\n    throw new TypeError('Arguments are invalid')\n  }\n\n  const alphabets = new Array(26)\n    .fill()\n    .map((_, index) => String.fromCharCode(97 + index)) // generate all lower alphabets array a-z\n\n  const cipherMap = alphabets.reduce(\n    (map, char, index) => map.set(char, alphabets[(rotation + index) % 26]),\n    new Map()\n  )\n\n  return str.replace(/[a-z]/gi, (char) => {\n    if (/[A-Z]/.test(char)) {\n      return cipherMap.get(char.toLowerCase()).toUpperCase()\n    }\n\n    return cipherMap.get(char)\n  })","sourceCodeStart":1,"sourceCodeEnd":29,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Ciphers/CaesarCipher.js#L1-L29","documentation":"Caesar cipher rejects the call when str is not a string OR rotation is not an integer OR rotation is negative. The three conditions are checked in one combined guard, so the message does not say which argument failed; all three must be valid (string + non-negative integer).","triggerScenarios":"str is a number/object, rotation is a float (3.5), a string (\"3\"), negative (-1), NaN, or undefined.","commonSituations":"Reading rotation from input/argv as a string, allowing a user-controlled negative shift, or omitting the rotation argument.","solutions":["Pass a string and a non-negative integer: caesarCipher('abc', 3).","Parse rotation: Math.abs(Math.trunc(Number(raw))).","Split-validate each argument before calling if you need a specific error message."],"exampleFix":"// before\ncaesarCipher(text, argv.shift) // string rotation\n// after\nconst shift = Math.abs(Math.trunc(Number(argv.shift)))\ncaesarCipher(String(text), shift)","handlingStrategy":"validation","validationCode":"function caesarSafe(text, raw) {\n  const rot = Math.abs(Math.trunc(Number(raw)));\n  return caesarCipher(String(text), Number.isInteger(rot) ? rot : 0);\n}","typeGuard":"/** @param {string} s @param {unknown} r @returns {boolean} */\nconst validCaesarArgs = (s, r) => typeof s === 'string' && Number.isInteger(r) && r >= 0;","tryCatchPattern":"try { return caesarCipher(text, rotation); }\ncatch (e) {\n  if (e instanceof TypeError && /Arguments are invalid/.test(e.message)) {\n    return caesarCipher(String(text), Math.abs(Math.trunc(Number(rotation))));\n  }\n  throw e;\n}","preventionTips":["Parse rotation from argv/input with Math.trunc(Number(...)) and Math.abs.","Validate str type and rotation integer/non-negative separately for clearer errors.","Default rotation to 0 when the source may be missing."],"tags":["cipher","caesar","type-validation","range-validation"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}