TheAlgorithms/JavaScript · error · Error
Both keyword and message must be specified
Error message
Both keyword and message must be specified
What it means
Thrown by checkInputs() when either keyword or message is falsy. The cipher needs both to operate: the keyword builds the shifted alphabet and the message is the text to translate. Any falsy value (empty string, null, undefined, 0) for either is rejected.
Source
Thrown at Ciphers/KeywordShiftedAlphabet.js:83
return encryptedAlphabet
}
function translate(sourceAlphabet, targetAlphabet, message) {
return message.split('').reduce((encryptedMessage, char) => {
const isUpperCase = char === char.toUpperCase()
const encryptedCharIndex = sourceAlphabet.indexOf(char.toLowerCase())
const encryptedChar =
encryptedCharIndex !== -1 ? targetAlphabet[encryptedCharIndex] : char
encryptedMessage += isUpperCase
? encryptedChar.toUpperCase()
: encryptedChar
return encryptedMessage
}, '')
}
function checkInputs(keyword, message) {
if (!keyword || !message) {
throw new Error('Both keyword and message must be specified')
}
if (!checkKeywordValidity(keyword)) {
throw new Error('Invalid keyword!')
}
}
function encrypt(keyword, message) {
checkInputs(keyword, message)
return translate(
alphabet,
getEncryptedAlphabet(keyword.toLowerCase()),
message
)
}
function decrypt(keyword, message) {
checkInputs(keyword, message)View on GitHub (pinned to 5c39e87a9a)
Solutions
- Provide non-empty keyword and message strings.
- Default both to '' only after confirming presence, or require them at the API boundary.
- Validate keyword.trim() and message.trim() before calling encrypt/decrypt.
Example fix
// before
encrypt(opts.keyword, opts.message) // one is undefined
// after
if (opts.keyword?.trim() && opts.message?.trim()) {
encrypt(opts.keyword, opts.message)
} Defensive patterns
Strategy: validation
Validate before calling
function encryptIfPresent(keyword, message) {
if (!keyword?.toString().trim() || !message?.toString().trim()) {
throw new Error('Both keyword and message must be specified');
}
return encrypt(keyword, message);
} Type guard
/** @param {unknown} k @param {unknown} m @returns {boolean} */
const hasBoth = (k, m) =>
typeof k === 'string' && typeof m === 'string' && k.trim() !== '' && m.trim() !== ''; Try / catch
try { return encrypt(keyword, message); }
catch (e) {
if (e instanceof Error && /must be specified/.test(e.message)) {
// require both fields in the UI before retrying
} else throw e;
} Prevention
- Require both fields in the form/UI before calling encrypt/decrypt.
- Trim and check non-empty at the boundary.
- Avoid passing destructured values that may be undefined.
When it happens
Trigger: Passing '' for keyword or message, null, undefined, 0, NaN, or omitting an argument (defaults to undefined).
Common situations: A form field left blank, an optional config key that defaulted to undefined, or destructuring that dropped one of the two required values.
Related errors
- Invalid keyword!
- Grid must be a non-empty array
- LFUCache ERROR: The Capacity is 0
- Coefficient a, b should be number
- Argument str should be String
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/04b9a868b0dc78fb.
Report an issue: GitHub.