Build a Currency Converter - Build a Currency Converter

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

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-currency-converter/67eaa957114d373deb3a9149.md at main · freeCodeCamp/freeCodeCamp · GitHub

Hi @inokchristine24

Changing the value of the second select element should not cause the converted amounts to be recalculated.

Changing the value of the second select element shouldn’t cause any change to the converted amount.

Happy coding

The tests here count how many times a useMemo callback runs, not just whether the number on screen is right. That’s why your version fails both even though the output looks correct.

You have two memos, so on mount there are already 2 calls. Test 5 expects exactly 2 calls in total after you change the first select (1 on mount + 1 for the change). Test 6 expects only the 1 mount call after you change the second select. Your second memo has toCurrency in its deps, so it fires again.

User story 5 is the hint: memoize “the converted amounts” (plural) for the from currency. Try one useMemo that depends only on amount and fromCurrency and returns the converted value for every currency in your list (an object keyed by currency code works well). Then the second select just looks up toCurrency in that object during render, with no memo around it, so changing it never re-runs the memo.

Keep .toFixed(2) on the displayed value so the XX.XX CCC format still passes.