index.jsx
const { useState, useMemo, useCallback } = React;
const exchangeRateMapping = {
"USD": 1,
"EUR": 0.86,
"GBP": 0.74,
"JPY": 148.46
};
export function CurrencyConverter() {
const [amount,setAmount] = useState(1);
const [fromCurrency,setFromCurrency] = useState("USD");
const [toCurrency,setToCurrency] = useState("EUR");
const baseAmount = useMemo(() =>{
console.log(`baseAmount is running`);
const base = amount/exchangeRateMapping[fromCurrency];
return base;
},[amount,fromCurrency]);
const finalAmount = useMemo(() =>{
console.log(`finalAmount is running`);
const final = baseAmount*exchangeRateMapping[toCurrency];
return final
},[toCurrency,baseAmount]);
return (
<div id="container">
<h1>Currency Converter</h1>
<div id="amount-container">
<label htmlFor="amount-input">
Convert : <input id="amount-input" type="number" value={amount} onChange={(e)=>setAmount(e.target.value)} />
</label>
</div>
<div id="from-currency-container">
<label htmlFor="from-currency">
From : <select value={fromCurrency} onChange={(e)=>setFromCurrency(e.target.value)}>
{Object.keys(exchangeRateMapping).map(currency=>(
<option key={currency} value={currency}>{currency}</option>
))}
</select>
</label>
</div>
<div id="to-currency-container">
<label htmlFor="to-currency">
To : <select value={toCurrency} onChange={(e)=>setToCurrency(e.target.value)}>
{Object.keys(exchangeRateMapping).map(currency=>(
<option key={currency} value={currency}>{currency}</option>
))}
</select>
</label>
</div>
<div id="results">
<p>Converted Amount : {finalAmount.toFixed(2)} {toCurrency}</p>
</div>
</div>
)
}
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Currency Converter</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.3.1/umd/react.development.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.3.1/umd/react-dom.development.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/7.26.5/babel.min.js"></script>
<script
data-plugins="transform-modules-umd"
type="text/babel"
src="index.jsx"
></script>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<div id="root"></div>
<script
data-plugins="transform-modules-umd"
type="text/babel"
data-presets="react"
data-type="module"
>
import { CurrencyConverter } from './index.jsx';
ReactDOM.createRoot(document.getElementById('root')).render(<CurrencyConverter />);
</script>
</body>
</html>
no css
Edit: no css
I am failing test cases
-
5. Changing the value of the first
selectelement should cause the converted amounts to be recalculated. -
6. Changing the value of the second
selectelement should not cause the converted amounts to be recalculated.
though I’m not sure why. With the console.log() statements, it seems like the code should pass the test case.
Any help would be appreciated! Thanks very much.