Currency Converter
A React exercise in deriving results from a single base rate and a small set of inputs.

I wanted one conversion rule that would work regardless of the selected currencies. I used the project to practice identifying the state a user controls and deriving the rest from it.
Normalizing all conversions through one base
The converter divides the amount by the source rate to get its USD equivalent, then multiplies that base by each destination rate. One table covers all available pairs. The React component memoizes the resulting map by amount and source currency. Changing only the destination reads another entry from the same map.
const convertCurrency = useMemo(() => {
//IF start currency is empty RETURN empty object
if(!strcurr) return {};
//CONVERT quantity to a number
//IF quantity is not a valid number RETURN empty object
const amount = Number(quantity);
if(!Number.isFinite(amount)) return {};
//GET the exchange rate for the start currency
//IF start rate does not exist RETURN empty object
const strRate = exchangeRate[strcurr];
if(!strRate) return {};
//CONVERT quantity to base currency (USD)
//base amount = quantity divided by start rate
const baseAmount = amount / strRate;
//CREATE empty object called result
const result = {};
//FOR EACH currency in exchange rate list:
//GET currency code
//GET currency rate
//CALCULATE converted amount:
//base amount multiplied by currency rate
//STORE result using currency code as key
for(const[code, rate] of Object.entries(exchangeRate)){
result[code] = baseAmount * rate
}
return result;
}, [quantity, strcurr]);
const converted = convertCurrency[trgcurr];Defining the limits of the exercise
The conversion guards unsupported source rates and non-finite input before doing the math. The rate table is fixed, which made it easy to compare expected calculations during the exercise. The numbers represent the prototype’s sample rates.
What I learned
Keeping conversion results derived from the inputs made the data flow clear. There was no second set of state to update when a user changed the currency selector.