{"record":{"id":"8b2588fe71de6f36","repo":"TheAlgorithms/JavaScript","slug":"improper-string-encoding","errorCode":null,"errorMessage":"Improper string encoding","messagePattern":"Improper string encoding","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Dynamic-Programming/FindMonthCalendar.js","lineNumber":53,"sourceCode":"      output(row)\n      if (dates.length === 0) break\n    }\n  }\n\n  parseDate(date) {\n    const dateAr = []\n    let block = ''\n    let i\n    for (i = 0; i < date.length; i++) {\n      if (date[i] === '/') {\n        dateAr.push(parseInt(block))\n        block = ''\n        continue\n      }\n      block += date[i]\n    }\n    dateAr.push(parseInt(block))\n    if (dateAr.length !== 2) throw new Error('Improper string encoding')\n    const dateOb = { month: dateAr[0], year: dateAr[1] }\n    return dateOb\n  }\n\n  isGreater(startDate, endDate) {\n    if (startDate.year > endDate.year) {\n      return true\n    } else if (startDate.year < endDate.year) {\n      return false\n    } else if (startDate.month > endDate.month) {\n      return true\n    } else if (startDate.month < endDate.month) {\n      return false\n    }\n    return true\n  }\n\n  getDayDiff(startDate, endDate) {","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Dynamic-Programming/FindMonthCalendar.js#L35-L71","documentation":"Thrown by Month.parseDate(date) (plain Error) when the parsed date does not yield exactly two components. The parser splits the string on '/' and pushes each block; it expects the format mm/yyyy (exactly one slash, two blocks). Any other number of slashes/blocks triggers the error.","triggerScenarios":"parseDate('1') (zero slashes -> 1 block); parseDate('1/2020/extra') (two slashes -> 3 blocks); parseDate('') (1 empty block); parseDate('1-2020') (no slash -> 1 block '1-2020').","commonSituations":"Passing dd/mm/yyyy (two slashes); passing ISO yyyy-mm-dd; missing the year; using dashes or dots instead of slashes; locale-formatted dates.","solutions":["Format input strictly as mm/yyyy with a single slash before calling parseDate.","Pre-validate with a regex: /^(0?[1-9]|1[0-2])\\/\\d{4}$/.","Normalize other formats (yyyy-mm-dd, dd/mm/yyyy) into mm/yyyy upstream.","Reject empty strings and strings without exactly one '/' before calling the parser."],"exampleFix":"// before\nmonth.parseDate('2020-01-15') // throws: not mm/yyyy\n\n// after\nconst m = String(rawDate.month).padStart(2, '0')\nconst y = String(rawDate.year)\nmonth.parseDate(`${m}/${y}`)","handlingStrategy":"validation","validationCode":"const MM_YYYY = /^(0?[1-9]|1[0-2])\\/\\d{4}$/\nfunction safeParseDate(month, monthObj) {\n  const m = String(monthObj.month).padStart(2, '0')\n  const y = String(monthObj.year)\n  const s = `${m}/${y}`\n  if (!MM_YYYY.test(s)) throw new Error(`expected mm/yyyy, got '${s}'`)\n  return month.parseDate(s)\n}","typeGuard":"const isMmYyyy = (s) => /^(0?[1-9]|1[0-2])\\/\\d{4}$/.test(s)","tryCatchPattern":"try {\n  return month.parseDate(input)\n} catch (e) {\n  if (e instanceof Error && /improper string encoding/i.test(e.message)) {\n    // normalize input to mm/yyyy and retry, or surface a friendly error\n  } else throw e\n}","preventionTips":["Format input strictly as mm/yyyy with a single slash before parsing.","Reject dd/mm/yyyy (two slashes) and ISO yyyy-mm-dd upstream.","Use a regex pre-check so users get a clear format error.","Don't pass raw user strings; build the string from numeric fields."],"tags":["dynamic-programming","date-parsing","input-format","calendar"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}