TheAlgorithms/JavaScript · error · Error
Invalid units
Error message
Invalid units
What it means
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.
Source
Thrown at Conversions/LengthConversion.js:26
* @throws {Error} If the units are invalid or not found in the conversion dictionary.
*/
const lengthConversion = (length, fromUnit, toUnit) => {
// Define a dictionary to map units to meters
const meters = {
mm: 0.001,
cm: 0.01,
m: 1,
km: 1000,
inch: 0.0254,
ft: 0.3048,
yd: 0.9144,
mi: 1609.34
}
// Check if the units are in the dictionary, otherwise, throw an error
if (!(fromUnit in meters) || !(toUnit in meters)) {
throw new Error('Invalid units')
}
// Perform the conversion
const metersFrom = length * meters[fromUnit]
const convertedLength = metersFrom / meters[toUnit]
return convertedLength
}
export { lengthConversion }
View on GitHub (pinned to 5c39e87a9a)
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.
Example fix
// before lengthConversion(1, 'meter', 'feet') // after lengthConversion(1, 'm', 'ft')
Defensive patterns
Strategy: validation
Validate before calling
const UNITS = new Set(['mm', 'cm', 'm', 'km', 'inch', 'ft', 'yd', 'mi'])
if (!UNITS.has(fromUnit) || !UNITS.has(toUnit)) {
throw new Error(`Unsupported unit. Allowed: ${[...UNITS].join(', ')}`)
}
lengthConversion(length, fromUnit, toUnit) Type guard
const SUPPORTED = new Set(['mm', 'cm', 'm', 'km', 'inch', 'ft', 'yd', 'mi']) const isSupportedUnit = (u) => SUPPORTED.has(u)
Try / catch
try {
lengthConversion(length, fromUnit, toUnit)
} catch (e) {
if (/Invalid units/.test(e.message)) {
// map common synonyms (meter->m, feet->ft) then retry
}
throw e
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Duplicate characters in character set are not allowed
- Not a valid character: ${digit}
- Argument is not a valid HEX code!
- Invalid hex string.
- Input is not a valid RGB color.
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/05c643b49ae09778.
Report an issue: GitHub.