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,
    message

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Pick a keyword with all unique letters: 'keyword', 'cipher', 'axiom'.
  2. 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.
  3. 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

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


AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13). Data as JSON: /api/errors/6370071f1c4ab4a4. Report an issue: GitHub.