Skip to content

Commit 5b77c79

Browse files
committed
feat: day 62
1 parent 55a84ca commit 5b77c79

File tree

1 file changed

+56
-8
lines changed

1 file changed

+56
-8
lines changed

062-exchange rate calculator/script.js

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,64 @@ const amountTwo = document.getElementById("amount-two");
55
const rate = document.getElementById("rate");
66
const swap = document.getElementById("swap");
77

8-
function calculate() {
8+
// function calculate() {
9+
// // Show a Loading State
10+
// rate.innerText = "Calculating...";
11+
// const currency_one = currencyOne.value;
12+
// const currency_two = currencyTwo.value;
13+
// // Cache API Results in sessionStorage
14+
// const cached = sessionStorage.getItem(currency_one);
15+
16+
// if (cached) {
17+
// const data = JSON.parse(cached);
18+
// const currentRate = data.rates[currency_two];
19+
// rate.innerText = `1 ${currency_one} = ${currentRate} ${currency_two}`;
20+
// amountTwo.value = (amountOne.value * currentRate).toFixed(2);
21+
// } else {
22+
// fetch(`https://api.exchangerate-api.com/v4/latest/${currency_one}`)
23+
// .then((res) => res.json())
24+
// .then((data) => {
25+
// sessionStorage.setItem(currency_one, JSON.stringify(data));
26+
// const currentRate = data.rates[currency_two];
27+
// rate.innerText = `1 ${currency_one} = ${currentRate} ${currency_two}`;
28+
// amountTwo.value = (amountOne.value * currentRate).toFixed(2);
29+
// })
30+
// // Add Error Handling for API Requests
31+
// .catch(() => {
32+
// rate.innerText = "Error: Could not fetch exchange rates.";
33+
// amountTwo.value = "0.00";
34+
// });
35+
// }
36+
// }
37+
38+
// Refactor to Use async/await
39+
async function calculate() {
40+
// Show a Loading State
41+
rate.innerText = "Calculating...";
942
const currency_one = currencyOne.value;
1043
const currency_two = currencyTwo.value;
11-
fetch(`https://api.exchangerate-api.com/v4/latest/${currency_one}`)
12-
.then((res) => res.json())
13-
.then((data) => {
14-
const currentRate = data.rates[currency_two];
15-
rate.innerText = `1 ${currency_one} = ${currentRate} ${currency_two}`;
16-
amountTwo.value = (amountOne.value * currentRate).toFixed(2);
17-
});
44+
// Cache API Results in sessionStorage
45+
const cached = sessionStorage.getItem(currency_one);
46+
47+
try {
48+
let data;
49+
if (cached) {
50+
data = JSON.parse(cached);
51+
} else {
52+
const res = await fetch(
53+
`https://api.exchangerate-api.com/v4/latest/${currency_one}`
54+
);
55+
data = await res.json();
56+
sessionStorage.setItem(currency_one, JSON.stringify(data));
57+
}
58+
const currentRate = data.rates[currency_two];
59+
rate.innerText = `1 ${currency_one} = ${currentRate} ${currency_two}`;
60+
amountTwo.value = (amountOne.value * currentRate).toFixed(2);
61+
// Add Error Handling for API Requests
62+
} catch (error) {
63+
rate.innerText = "Error: Could not fetch exchange rates.";
64+
amountTwo.value = "0.00";
65+
}
1866
}
1967

2068
currencyOne.addEventListener("change", calculate);

0 commit comments

Comments
 (0)