TheAlgorithms/JavaScript · error · TypeError
Arguments are invalid
Error message
Arguments are invalid
What it means
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).
Source
Thrown at Ciphers/CaesarCipher.js:11
/**
* @function caesarsCipher
* @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.
* @see - [wiki](https://en.wikipedia.org/wiki/Caesar_cipher)
* @param {string} str - string to be encrypted
* @param {number} rotation - the number of rotation, expect real number ( > 0)
* @return {string} - decrypted string
*/
const caesarCipher = (str, rotation) => {
if (typeof str !== 'string' || !Number.isInteger(rotation) || rotation < 0) {
throw new TypeError('Arguments are invalid')
}
const alphabets = new Array(26)
.fill()
.map((_, index) => String.fromCharCode(97 + index)) // generate all lower alphabets array a-z
const cipherMap = alphabets.reduce(
(map, char, index) => map.set(char, alphabets[(rotation + index) % 26]),
new Map()
)
return str.replace(/[a-z]/gi, (char) => {
if (/[A-Z]/.test(char)) {
return cipherMap.get(char.toLowerCase()).toUpperCase()
}
return cipherMap.get(char)
})View on GitHub (pinned to 5c39e87a9a)
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.
Example fix
// before caesarCipher(text, argv.shift) // string rotation // after const shift = Math.abs(Math.trunc(Number(argv.shift))) caesarCipher(String(text), shift)
Defensive patterns
Strategy: validation
Validate before calling
function caesarSafe(text, raw) {
const rot = Math.abs(Math.trunc(Number(raw)));
return caesarCipher(String(text), Number.isInteger(rot) ? rot : 0);
} Type guard
/** @param {string} s @param {unknown} r @returns {boolean} */
const validCaesarArgs = (s, r) => typeof s === 'string' && Number.isInteger(r) && r >= 0; Try / catch
try { return caesarCipher(text, rotation); }
catch (e) {
if (e instanceof TypeError && /Arguments are invalid/.test(e.message)) {
return caesarCipher(String(text), Math.abs(Math.trunc(Number(rotation))));
}
throw e;
} Prevention
- 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.
When it happens
Trigger: str is a number/object, rotation is a float (3.5), a string ("3"), negative (-1), NaN, or undefined.
Common situations: Reading rotation from input/argv as a string, allowing a user-controlled negative shift, or omitting the rotation argument.
Related errors
- Coefficient a, b should be number
- Argument str should be String
- Argument should be string
- Argument should be string
- Arguments type are invalid
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/640162b7292d4354.
Report an issue: GitHub.