CodingNic

Build the Currency Converter

Build the Converter Update Cycle

Build the Currency Converter 18 min read

Build the Converter Update Cycle

Build the Converter Update Cycle

Open app.js and add the update() function. This function is the center of the converter: it reads the current state and writes the current state back into the UI.

Start with the selected currencies:

javascript
function update() {
  $("fromFlag").textContent = currencies[from][0];
  $("fromCode").textContent = from;
  $("toFlag").textContent = currencies[to][0];
  $("toCode").textContent = to;

Then calculate the result:

javascript
  const r = rate(from, to);
  const amount = Number($("amount").value) || 0;
  $("result").value = fmt(amount * r);
  $("rateText").textContent = `1 ${from} = ${fmt(r)} ${to}`;
}

Finish the function by keeping the chart pair label and Popular Currencies in sync with the same state.

Test it

Type a different amount in the Send field. The receive field should update immediately.

Checkpoint

Change both currencies and the amount. The receive value and 1 FROM = RATE TO line should always match the selected pair.