Skip to content

Unit Converter

Easy~25 min

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. 1

    Write the length conversions

    Define kmToMiles(km) and milesToKm(mi). The factor is 1 km ≈ 0.621371 miles. Keeping each conversion in its own function makes them easy to test and reuse.

    Code
    function 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. 2

    Write the temperature conversions

    Define celsiusToFahrenheit(c) and fahrenheitToCelsius(f). The formulas are F = C*9/5 + 32 and C = (F - 32) * 5/9.

    Code
    function 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. 3

    Build a dispatcher

    Create an object converters whose keys are conversion names and whose values are the functions. A convert(name, value) helper looks up the function and calls it. This pattern scales to many conversions without a giant if/else.

    Code
    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);
    }

    Expected result

    convert("km-to-miles", 10) returns 6.21371; convert("c-to-f", 100) returns 212.

  4. 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.

    Code
    console.log(milesToKm(kmToMiles(42)));       // ≈ 42
    console.log(fahrenheitToCelsius(celsiusToFahrenheit(25))); // ≈ 25

    Expected result

    Both lines print a number very close to 42 and 25 respectively.

Try it yourself

What 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

  • Functions

    Functions are the unit of reuse in this project.

  • Objects

    Objects as lookup tables — the dispatcher pattern.

Related Practice

Related Tools

Related Cheatsheets

Related Reference

Related Errors

Related Glossary

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.

← Back to topic