{"record":{"id":"582aa0eaa93069d6","repo":"TheAlgorithms/JavaScript","slug":"argument-should-be-a-string","errorCode":null,"errorMessage":"Argument should be a string","messagePattern":"Argument should be a string","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"String/MaxCharacter.js","lineNumber":11,"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) {","sourceCodeStart":1,"sourceCodeEnd":29,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/String/MaxCharacter.js#L1-L29","documentation":"First guard in maxCharacter. The function returns the most-frequent character (optionally ignoring a RegExp pattern) and requires str to be a string, throwing TypeError otherwise. This is the type branch; the emptiness branch is a separate error ([156]). Note the empty-string check (!str) is unreachable from a non-string because typeof short-circuits first.","triggerScenarios":"Calling maxCharacter(undefined), maxCharacter(null), maxCharacter(0), maxCharacter({}), maxCharacter([]). Any input where typeof !== 'string'.","commonSituations":"A field that is null on miss; a value coerced to a number; an array passed where a string was expected; refactoring that dropped the argument.","solutions":["Pass a string; default missing values to '' (note '' triggers the empty-string error, so pick a real default).","Validate typeof at the call site.","If the source is an array, join first."],"exampleFix":"// before\nmaxCharacter(maybeStr)\n\n// after\nif (typeof maybeStr === 'string' && maybeStr.length) maxCharacter(maybeStr)","handlingStrategy":"type-guard","validationCode":"if (typeof str !== 'string') {\n  throw new TypeError('str must be a string')\n}\nmaxCharacter(str)","typeGuard":"const isString = (v) => typeof v === 'string'","tryCatchPattern":"try {\n  maxCharacter(input)\n} catch (e) {\n  if (e instanceof TypeError && /should be a string/i.test(e.message)) { /* not a string */ } else throw e\n}","preventionTips":["Validate typeof at the boundary.","Also guard for emptiness to avoid the follow-up [156] error.","Join arrays before passing."],"tags":["string","validation","type-guard","max-character"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}