{"record":{"id":"7a2d91d664ca0880","repo":"TheAlgorithms/JavaScript","slug":"input-must-not-contain-numbers","errorCode":null,"errorMessage":"Input must not contain numbers","messagePattern":"Input must not contain numbers","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"String/CountLetters.js","lineNumber":21,"sourceCode":" * @description Given a string, count the number of each letter.\n * @param {String} str - The input string\n * @return {Object} - Object with letters and number of times\n * @example countLetters(\"hello\") => {h: 1, e: 1, l: 2, o: 1}\n */\n\nconst countLetters = (str) => {\n  const specialChars = /\\W/g\n\n  if (typeof str !== 'string') {\n    throw new TypeError('Input should be a string')\n  }\n\n  if (specialChars.test(str)) {\n    throw new TypeError('Input must not contain special characters')\n  }\n\n  if (/\\d/.test(str)) {\n    throw new TypeError('Input must not contain numbers')\n  }\n\n  const obj = {}\n  for (let i = 0; i < str.toLowerCase().length; i++) {\n    const char = str.toLowerCase().charAt(i)\n    obj[char] = (obj[char] || 0) + 1\n  }\n\n  return obj\n}\n\nexport { countLetters }\n","sourceCodeStart":3,"sourceCodeEnd":34,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/String/CountLetters.js#L3-L34","documentation":"Third guard in countLetters, fired by /\\d/. After the type check and the special-character check, any decimal digit (0-9) throws TypeError. Combined with the previous guard this means countLetters only accepts pure ASCII letters [A-Za-z] (and underscores, which survive \\W but produce an underscore key).","triggerScenarios":"Calling countLetters('abc123'), countLetters('a1b2'), countLetters('2024'). Any string containing a digit that already passed the special-char check.","commonSituations":"Alphanumeric identifiers (usernames, codes) passed where letters-only was assumed; alphanumeric reference numbers; forgetting the function is letters-only despite its name.","solutions":["Strip digits first: str.replace(/\\d/g, '').","Use a more permissive counter if you need digits included.","Pre-validate the input is letters-only: /^[A-Za-z]+$/.test(str)."],"exampleFix":"// before\ncountLetters('abc123')\n\n// after\ncountLetters('abc123'.replace(/\\d/g, ''))","handlingStrategy":"validation","validationCode":"const clean = str.replace(/\\d/g, '')\ncountLetters(clean)","typeGuard":"const hasNoDigits = (s) => typeof s === 'string' && !/\\d/.test(s)","tryCatchPattern":"try {\n  countLetters(str)\n} catch (e) {\n  if (e instanceof TypeError && /numbers/i.test(e.message)) {\n    countLetters(str.replace(/\\d/g, ''))\n  } else throw e\n}","preventionTips":["Strip digits when counting letters only.","Pre-validate with /^[A-Za-z]+$/.test(str).","Remember underscores survive both guards and become a counted key."],"tags":["string","validation","regex","letter-count","ascii-only"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}