{"record":{"id":"d6c91b54116e60e6","repo":"TheAlgorithms/JavaScript","slug":"the-param-should-be-string","errorCode":null,"errorMessage":"the param should be string","messagePattern":"the param should be string","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"String/MaxWord.js","lineNumber":12,"sourceCode":"// Given a sentence, return the most occurring word\n\n/**\n * @param {string} sentence - the sentence you want to find the most occurring word\n * @returns {string} - the most occurring word\n *\n * @example\n *     -  maxWord('lala lili lala'); // lala\n */\nconst maxWord = (sentence = '') => {\n  if (typeof sentence !== 'string') {\n    throw new TypeError('the param should be string')\n  }\n\n  if (!sentence) {\n    return null\n  }\n\n  const words = sentence.split(' ')\n  if (words.length < 2) {\n    return words[0]\n  }\n\n  const occurrences = {}\n  words.forEach((word) => {\n    occurrences[word.toLocaleLowerCase()] =\n      occurrences[word.toLocaleLowerCase()] + 1 || 1\n  })\n\n  const max = Object.keys(occurrences).reduce(","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/String/MaxWord.js#L1-L30","documentation":"Guard in maxWord. The function finds the most-occurring word in a sentence and requires sentence to be a string, throwing TypeError otherwise. The parameter defaults to '', so omitting it is safe; only an explicitly non-string value throws.","triggerScenarios":"Calling maxWord(null), maxWord(42), maxWord(['a','b']), maxWord({}). Any explicit non-string argument. (maxWord() and maxWord(undefined) do NOT throw because the default applies.)","commonSituations":"A source that returns null on miss instead of undefined (bypassing the default); a number passed where a sentence string was expected; an array passed where a joined string was intended.","solutions":["Coerce null/undefined to '' before calling: sentence ?? ''.","Validate typeof at the call site.","Join arrays first: arr.join(' ')."],"exampleFix":"// before\nmaxWord(maybeSentence)\n\n// after\nmaxWord(typeof maybeSentence === 'string' ? maybeSentence : '')","handlingStrategy":"type-guard","validationCode":"if (typeof sentence !== 'string') {\n  throw new TypeError('sentence must be a string')\n}\nmaxWord(sentence)","typeGuard":"const isString = (v) => typeof v === 'string'","tryCatchPattern":"try {\n  maxWord(sentence)\n} catch (e) {\n  if (e instanceof TypeError) { /* not a string */ } else throw e\n}","preventionTips":["Coalesce null to '': sentence ?? '' (the default only catches undefined).","Join arrays before passing.","Validate typeof at the boundary."],"tags":["string","validation","type-guard","word-count","default-arg"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}