Tell us what’s happening:
these tests are failing
5. Changing the value of the first select element should cause the converted amounts to be recalculated.
6. Changing the value of the second select element should not cause the converted amounts to be recalculated.
Your code so far
<!-- file: index.html -->
/* file: styles.css */
/* file: index.jsx */
const { useState, useMemo } = React;
export function CurrencyConverter() {
const [amount, setAmount] = useState(0);
const [fromCurrency, setFromCurrency] = useState("USD");
const [toCurrency, setToCurrency] = useState("EUR");
const currencies = [
{ currency: "USD", value: 1 },
{ currency: "EUR", value: 0.92 },
{ currency: "GBP", value: 0.78 },
{ currency: "JPY", value: 156.7 }
];
// Memoize the amount in the from currency.
// Changing toCurrency will not recalculate this value.
const fromAmount = useMemo(() => {
const fromRate = currencies.find(
(i) => i.currency === fromCurrency
).value;
return Number(amount) / fromRate;
}, [amount, fromCurrency]);
// Calculate the converted amount.
const result = useMemo(() => {
const toRate = currencies.find(
(i) => i.currency === toCurrency
).value;
return (fromAmount * toRate).toFixed(2);
}, [fromAmount, toCurrency]);
return (
<div className="container">
<h2>Currency Converter</h2>
<label>
Enter value:
<input
type="number"
value={amount}
onChange={(e) => setAmount(e.target.value)}
/>
</label>
<label>
Convert From:
<select
value={fromCurrency}
onChange={(e) => setFromCurrency(e.target.value)}
>
{currencies.map((i) => (
<option key={i.currency} value={i.currency}>
{i.currency}
</option>
))}
</select>
</label>
<label>
Convert To:
<select
value={toCurrency}
onChange={(e) => setToCurrency(e.target.value)}
>
{currencies.map((i) => (
<option key={i.currency} value={i.currency}>
{i.currency}
</option>
))}
</select>
</label>
<p className="result">
{result} {toCurrency}
</p>
</div>
);
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/155.0.0.0 Safari/537.36
Challenge Information:
Build a Currency Converter - Build a Currency Converter