Unit Converter
What you'll build
A small library of conversion functions — kilometers to miles, Celsius to Fahrenheit, and back — plus a dispatcher that picks the right conversion by name.
Goals
- Write pure functions that map one value to another
- Use a lookup object to dispatch by string key
- Practice inverse conversions (round-trip)
- Organize related functions into one module
Prerequisites
- Functions and return values
- Objects / dictionaries as lookup tables
- Basic arithmetic and unit awareness
Steps
- 1
Write the length conversions
Define
kmToMiles(km)andmilesToKm(mi). The factor is 1 km ≈ 0.621371 miles. Keeping each conversion in its own function makes them easy to test and reuse.Codefunction kmToMiles(km) { return km * 0.621371; } function milesToKm(mi) { return mi / 0.621371; }Expected result
kmToMiles(10) returns about 6.21371; milesToKm(6.21371) returns about 10.
- 2
Write the temperature conversions
Define
celsiusToFahrenheit(c)andfahrenheitToCelsius(f). The formulas are F = C*9/5 + 32 and C = (F - 32) * 5/9.Codefunction celsiusToFahrenheit(c) { return c * 9 / 5 + 32; } function fahrenheitToCelsius(f) { return (f - 32) * 5 / 9; }Expected result
celsiusToFahrenheit(0) returns 32; fahrenheitToCelsius(98.6) returns about 37.
- 3
Build a dispatcher
Create an object
converterswhose keys are conversion names and whose values are the functions. Aconvert(name, value)helper looks up the function and calls it. This pattern scales to many conversions without a giant if/else.Codeconst converters = { "km-to-miles": kmToMiles, "miles-to-km": milesToKm, "c-to-f": celsiusToFahrenheit, "f-to-c": fahrenheitToCelsius, }; function convert(name, value) { const fn = converters[name]; if (!fn) return "Unknown conversion"; return fn(value); }Expected result
convert("km-to-miles", 10) returns 6.21371; convert("c-to-f", 100) returns 212.
- 4
Verify round-trips
A good conversion is invertible: converting back should return the original value (within rounding). Test one length and one temperature round-trip.
Codeconsole.log(milesToKm(kmToMiles(42))); // ≈ 42 console.log(fahrenheitToCelsius(celsiusToFahrenheit(25))); // ≈ 25Expected result
Both lines print a number very close to 42 and 25 respectively.
Hint
Floating-point math is not exact — small differences like 41.99999 are expected.
Try it yourself
// TODO: implement kmToMiles, milesToKm, celsiusToFahrenheit, fahrenheitToCelsius
// Then build a `converters` object and a `convert(name, value)` dispatcher.
function convert(name, value) {
// TODO: look up the function in converters and call it
}function kmToMiles(km) { return km * 0.621371; }
function milesToKm(mi) { return mi / 0.621371; }
function celsiusToFahrenheit(c) { return c * 9 / 5 + 32; }
function fahrenheitToCelsius(f) { return (f - 32) * 5 / 9; }
const converters = {
"km-to-miles": kmToMiles,
"miles-to-km": milesToKm,
"c-to-f": celsiusToFahrenheit,
"f-to-c": fahrenheitToCelsius,
};
function convert(name, value) {
const fn = converters[name];
if (!fn) return "Unknown conversion";
return fn(value);
}
console.log(convert("km-to-miles", 10)); // 6.21371
console.log(convert("c-to-f", 100)); // 212What you learned
You composed pure functions into a small library and used a lookup object as a dispatcher — a pattern that scales far better than long if/else chains.
Related Resources
Related Lessons
Related Practice
- Functions Find
Reading and tracing small functions like these converters.
- Data Types Predict
Predict the type returned by arithmetic expressions.
Related Tools
- Temperature Converter
A production tool that does the same conversions in the browser.
- Length / Base Converter
Another unit-style converter to compare patterns with.
Related Cheatsheets
- JavaScript Cheatsheet
Function and object syntax reference.
Related Reference
- JavaScript Math
Arithmetic and number conversion reference.
Related Errors
- Type Error
Operating on incompatible types.
Related Glossary
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Related Errors
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
- Type Error
Operating on incompatible types.