{"record":{"id":"fc7571d64a97899a","repo":"TheAlgorithms/JavaScript","slug":"the-param-should-be-a-nonempty-string","errorCode":null,"errorMessage":"The param should be a nonempty string","messagePattern":"The param should be a nonempty string","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"String/MaxCharacter.js","lineNumber":13,"sourceCode":"/**\n * @function maxCharacter\n * @example - Given a string of characters, return the character that appears the most often. Example: input = \"Hello World!\" return \"l\"\n * @param {string} str\n * @param {RegExp} ignorePattern - ignore the char in str that is not required\n * @returns {string} - char\n */\nconst maxCharacter = (str, ignorePattern) => {\n  // initially it's count only alphabets\n  if (typeof str !== 'string') {\n    throw new TypeError('Argument should be a string')\n  } else if (!str) {\n    throw new Error('The param should be a nonempty string')\n  }\n\n  // store all char in occurrence map\n  const occurrenceMap = new Map()\n\n  for (const char of str) {\n    if (!ignorePattern?.test(char)) {\n      occurrenceMap.set(char, occurrenceMap.get(char) + 1 || 1)\n    }\n  }\n\n  // find the max char from the occurrence map\n  let max = { char: '', occur: -Infinity }\n\n  for (const [char, occur] of occurrenceMap) {\n    if (occur > max.occur) {\n      max = { char, occur }\n    }","sourceCodeStart":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/String/MaxCharacter.js#L1-L31","documentation":"Second guard in maxCharacter. After the typeof check passes, if str is falsy (empty string '', but also NaN/0/false are already blocked by the typeof check, so in practice only '') the function throws a plain Error. The intent is to reject empty input because there is no 'max' character to return.","triggerScenarios":"Calling maxCharacter(''), maxCharacter('') with any ignorePattern. Only an empty string reaches this branch.","commonSituations":"Empty form input forwarded without trimming/validation; a string field that is '' by default; whitespace stripped down to '' before the call; an array joined to '' .","solutions":["Guard for emptiness before calling: only call when str.length > 0.","Trim and re-check if whitespace-only input should be treated as empty.","Provide a meaningful fallback (skip the call) instead of letting it throw."],"exampleFix":"// before\nmaxCharacter(text)\n\n// after\nconst t = (typeof text === 'string' ? text : '').trim()\nif (t.length) maxCharacter(t)","handlingStrategy":"validation","validationCode":"const text = (typeof str === 'string' ? str : '').trim()\nif (text.length === 0) {\n  throw new Error('str must be a non-empty string')\n}\nmaxCharacter(text)","typeGuard":"const isNonEmptyString = (v) => typeof v === 'string' && v.trim().length > 0","tryCatchPattern":"try {\n  maxCharacter(str)\n} catch (e) {\n  if (e instanceof Error && /nonempty/i.test(e.message)) { /* provide default or skip */ } else throw e\n}","preventionTips":["Trim and length-check before calling.","Treat whitespace-only as empty.","Skip the call rather than letting it throw on ''."],"tags":["string","validation","empty-input","max-character"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}