{"record":{"id":"6370071f1c4ab4a4","repo":"TheAlgorithms/JavaScript","slug":"invalid-keyword","errorCode":null,"errorMessage":"Invalid keyword!","messagePattern":"Invalid keyword!","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Ciphers/KeywordShiftedAlphabet.js","lineNumber":87,"sourceCode":"  return message.split('').reduce((encryptedMessage, char) => {\n    const isUpperCase = char === char.toUpperCase()\n    const encryptedCharIndex = sourceAlphabet.indexOf(char.toLowerCase())\n    const encryptedChar =\n      encryptedCharIndex !== -1 ? targetAlphabet[encryptedCharIndex] : char\n    encryptedMessage += isUpperCase\n      ? encryptedChar.toUpperCase()\n      : encryptedChar\n    return encryptedMessage\n  }, '')\n}\n\nfunction checkInputs(keyword, message) {\n  if (!keyword || !message) {\n    throw new Error('Both keyword and message must be specified')\n  }\n\n  if (!checkKeywordValidity(keyword)) {\n    throw new Error('Invalid keyword!')\n  }\n}\n\nfunction encrypt(keyword, message) {\n  checkInputs(keyword, message)\n  return translate(\n    alphabet,\n    getEncryptedAlphabet(keyword.toLowerCase()),\n    message\n  )\n}\n\nfunction decrypt(keyword, message) {\n  checkInputs(keyword, message)\n  return translate(\n    getEncryptedAlphabet(keyword.toLowerCase()),\n    alphabet,\n    message","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Ciphers/KeywordShiftedAlphabet.js#L69-L105","documentation":"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.","triggerScenarios":"Intended: keywords with repeated letters like 'hello' (double l), 'banana', or 'letter'. Actual current behavior: never thrown due to the forEach bug.","commonSituations":"Users naturally pick words with repeated letters; the intended guard would catch them, but today they slip through and silently corrupt the cipher alphabet.","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."],"exampleFix":"// before (library bug lets this pass silently)\nencrypt('hello', msg) // duplicate 'l'\n// after (call-site guard the library lacks)\nconst hasUniqueChars = s => new Set(s).size === s.length\nif (hasUniqueChars(keyword)) encrypt(keyword, msg)","handlingStrategy":"validation","validationCode":"// The library's checkKeywordValidity is buggy and never throws this.\n// Validate uniqueness yourself:\nconst hasUniqueChars = s => new Set(s).size === s.length;\nfunction encryptSafe(keyword, message) {\n  if (!hasUniqueChars(keyword)) throw new Error('Invalid keyword!');\n  return encrypt(keyword, message);\n}","typeGuard":"/** @param {unknown} k @returns {boolean} */\nconst isUniqueKeyword = k =>\n  typeof k === 'string' && k.length > 0 && new Set(k).size === k.length;","tryCatchPattern":"try { return encrypt(keyword, message); }\ncatch (e) {\n  if (e instanceof Error && /Invalid keyword/.test(e.message)) {\n    // de-duplicate letters: keyword = [...new Set(keyword)].join('')\n  } else throw e;\n}","preventionTips":["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."],"tags":["cipher","keyword-shifted","input-validation","bug","duplicate-characters"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}