{"record":{"id":"05c643b49ae09778","repo":"TheAlgorithms/JavaScript","slug":"invalid-units","errorCode":null,"errorMessage":"Invalid units","messagePattern":"Invalid units","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Conversions/LengthConversion.js","lineNumber":26,"sourceCode":" * @throws {Error} If the units are invalid or not found in the conversion dictionary.\n */\n\nconst lengthConversion = (length, fromUnit, toUnit) => {\n  // Define a dictionary to map units to meters\n  const meters = {\n    mm: 0.001,\n    cm: 0.01,\n    m: 1,\n    km: 1000,\n    inch: 0.0254,\n    ft: 0.3048,\n    yd: 0.9144,\n    mi: 1609.34\n  }\n\n  // Check if the units are in the dictionary, otherwise, throw an error\n  if (!(fromUnit in meters) || !(toUnit in meters)) {\n    throw new Error('Invalid units')\n  }\n\n  // Perform the conversion\n  const metersFrom = length * meters[fromUnit]\n  const convertedLength = metersFrom / meters[toUnit]\n\n  return convertedLength\n}\n\nexport { lengthConversion }\n","sourceCodeStart":8,"sourceCodeEnd":37,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Conversions/LengthConversion.js#L8-L37","documentation":"Thrown by lengthConversion when fromUnit or toUnit is not a key in the internal meters dictionary. Supported keys are exactly: mm, cm, m, km, inch, ft, yd, mi. The check uses the 'in' operator, so both the source and target unit must match one of those literal strings.","triggerScenarios":"Calling lengthConversion(1, 'meter', 'feet') (full names unsupported), lengthConversion(1, 'in', 'cm') ('in' is not 'inch'), lengthConversion(1, 'KG', 'm'), or lengthConversion(1, 'miles', 'km'). Even 'inches' (plural) fails.","commonSituations":"Using full unit names instead of the short codes; pluralizing ('inches', 'feet'); using 'in' abbreviation for inch (the API wants 'inch'); passing uppercase; mismapping imperial vs metric keys.","solutions":["Use only the documented keys: mm, cm, m, km, inch, ft, yd, mi.","Map your own unit strings to these keys before calling, e.g. { meter: 'm', meters: 'm', feet: 'ft' }.","Lowercase and trim the unit strings to avoid case/whitespace mismatches."],"exampleFix":"// before\nlengthConversion(1, 'meter', 'feet')\n// after\nlengthConversion(1, 'm', 'ft')","handlingStrategy":"validation","validationCode":"const UNITS = new Set(['mm', 'cm', 'm', 'km', 'inch', 'ft', 'yd', 'mi'])\nif (!UNITS.has(fromUnit) || !UNITS.has(toUnit)) {\n  throw new Error(`Unsupported unit. Allowed: ${[...UNITS].join(', ')}`)\n}\nlengthConversion(length, fromUnit, toUnit)","typeGuard":"const SUPPORTED = new Set(['mm', 'cm', 'm', 'km', 'inch', 'ft', 'yd', 'mi'])\nconst isSupportedUnit = (u) => SUPPORTED.has(u)","tryCatchPattern":"try {\n  lengthConversion(length, fromUnit, toUnit)\n} catch (e) {\n  if (/Invalid units/.test(e.message)) {\n    // map common synonyms (meter->m, feet->ft) then retry\n  }\n  throw e\n}","preventionTips":["Use only the documented short keys: mm, cm, m, km, inch, ft, yd, mi.","Build a synonym map from your own unit names to the supported keys.","Lowercase and trim unit strings at the boundary."],"tags":["validation","units","length","lookup","conversions"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}