TheAlgorithms/JavaScript · error · Error
Invalid keyword!
Error message
Invalid keyword!
What it means
Intended to be thrown by checkInputs() when the keyword contains duplicate characters: the shifted-alphabet construction needs each keyword letter to appear exactly once. IMPORTANT: the underlying checkKeywordValidity() has a bug — its `return false` is inside a forEach callback (which ignores return values), so the function ALWAYS returns true and this error is effectively UNREACHABLE in the current code. Duplicate-letter keywords currently pass silently and produce a malformed alphabet.
Source
Thrown at Ciphers/KeywordShiftedAlphabet.js:87
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)
return translate(
getEncryptedAlphabet(keyword.toLowerCase()),
alphabet,
messageView on GitHub (pinned to 5c39e87a9a)
Solutions
- Pick a keyword with all unique letters: 'keyword', 'cipher', 'axiom'.
- If maintaining this code, FIX checkKeywordValidity to use a Set or some()/indexOf loop that actually returns false: e.g. new Set(keyword).size === keyword.length.
- Until fixed, validate uniqueness yourself at the call site since the library will not.
Example fix
// before (library bug lets this pass silently)
encrypt('hello', msg) // duplicate 'l'
// after (call-site guard the library lacks)
const hasUniqueChars = s => new Set(s).size === s.length
if (hasUniqueChars(keyword)) encrypt(keyword, msg) Defensive patterns
Strategy: validation
Validate before calling
// The library's checkKeywordValidity is buggy and never throws this.
// Validate uniqueness yourself:
const hasUniqueChars = s => new Set(s).size === s.length;
function encryptSafe(keyword, message) {
if (!hasUniqueChars(keyword)) throw new Error('Invalid keyword!');
return encrypt(keyword, message);
} Type guard
/** @param {unknown} k @returns {boolean} */
const isUniqueKeyword = k =>
typeof k === 'string' && k.length > 0 && new Set(k).size === k.length; Try / catch
try { return encrypt(keyword, message); }
catch (e) {
if (e instanceof Error && /Invalid keyword/.test(e.message)) {
// de-duplicate letters: keyword = [...new Set(keyword)].join('')
} else throw e;
} Prevention
- Pick keywords with no repeated letters ('keyword', 'cipher', 'axiom').
- Validate with new Set(kw).size === kw.length yourself — the library currently will not.
- If maintaining the library, fix checkKeywordValidity to return from the function, not the forEach callback.
When it happens
Trigger: Intended: keywords with repeated letters like 'hello' (double l), 'banana', or 'letter'. Actual current behavior: never thrown due to the forEach bug.
Common situations: Users naturally pick words with repeated letters; the intended guard would catch them, but today they slip through and silently corrupt the cipher alphabet.
Related errors
- Both keyword and message must be specified
- 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/6370071f1c4ab4a4.
Report an issue: GitHub.