Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions complex-api-2/css/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
body {
font-family: "Arial", sans-serif;
margin: 0;
padding: 0;
background-color: #f4f6f9;
}

header {
background-color: #1b2838;
color: white;
padding: 10px 20px;
border-bottom: 2px solid #ccc;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

nav {
display: flex;
justify-content: space-between;
align-items: center;
}

nav .logo {
font-size: 24px;
font-weight: bold;
display: flex;
align-items: center;
gap: 10px;
}

nav ul {
display: flex;
list-style: none;
}

nav li {
margin: 0 30px;
font-size: 16px;
cursor: pointer;
display: flex;
align-items: center;
gap: 5px;
color: white;
transition: color 0.3s ease;
}

nav li:hover {
color: #2ecc71;
}

main {
text-align: center;
padding: 40px 20px;
}

main h1 {
font-size: 32px;
font-weight: bold;
color: #1b2838;
margin-bottom: 20px;
}

label {
display: block;
font-weight: bold;
margin: 10px 0;
}

input[type="text"] {
width: 20%;
padding: 10px;
margin-bottom: 20px;
border: 2px solid #1b2838;
border-radius: 5px;
}

button {
background-color: #1b2838;
color: white;
padding: 10px 20px;
font-size: 16px;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s ease;
}

button:hover {
background-color: #2ecc71;
color: white;
}

footer {
background-color: #1b2838;
color: white;
text-align: center;
padding: 10px 0;
margin-top: 40px;
}

ul {
list-style-type: none;
margin: 20px auto;
padding: 0;
width: 70%;
text-align: left;
}
35 changes: 35 additions & 0 deletions complex-api-2/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="description" content="This is where your description goes">
<meta name="keywords" content="one, two, three">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Complex API #2</title>
<!-- external CSS link -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" rel="stylesheet">
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<header>
<nav>
<div class="logo">
<i class="fas fa-chart-line"></i> Finance Tracker
</div>
<ul>
<li><i class="fas fa-home"></i> Home</li>
<li><i class="fas fa-money-bill-wave"></i> Exchange Rates</li>
<li><i class="fas fa-info-circle"></i> About</li>
<li><i class="fas fa-envelope"></i> Contact</li>
</ul>
</nav>
</header>
<h1>Get the currency exchange by country</h1>
<label for="location">Enter a country:</label>
<input type="text" id="country" placeholder="e.g., Spain, Bangladesh, South Africa" required>
<button type="button" name="button">currency</button>
<ul id="countryUl"></ul>
<ul id="exchangeUl"></ul>
<script type="text/javascript" src="js/main.js"></script>
</body>
</html>
60 changes: 60 additions & 0 deletions complex-api-2/js/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
document.querySelector('button').addEventListener('click', getCountryData);

function getCountryData() {
const countryName = document.getElementById('country').value;

const restCountriesUrl = `https://restcountries.com/v3.1/name/${countryName}`;

fetch(restCountriesUrl)
.then(response => response.json())
.then(data => {
// Extract currency code from the REST Countries API response
const country = data[0];
console.log(country);

const currencyCode = Object.keys(country.currencies)[0]; // Get the first currency code
const currencyName = country.currencies[currencyCode].name;

console.log(`Currency Code: ${currencyCode}, Currency Name: ${currencyName}`);

// Display basic country info
const countryInfo = document.querySelector('#countryUl');
countryInfo.innerHTML = `
<li>Country: ${country.name.common}</li>
<li>Currency: ${currencyName} (${currencyCode})</li>
<li>Region: ${country.region}</li>
<li>Population: ${country.population.toLocaleString()}</li>
`;

// Call the Exchange Rate API with the currency code
getExchangeRate(currencyCode);
})
.catch(err => {
console.error(`Error fetching country data: ${err}`);
});
}

function getExchangeRate(currencyCode) {
const exchangeRateApiKey = 'af01377e51c0ab6ed3045b88'; // Replace with your actual API key
const exchangeRateUrl = `https://v6.exchangerate-api.com/v6/${exchangeRateApiKey}/latest/${currencyCode}`;

fetch(exchangeRateUrl)
.then(response => response.json())
.then(data => {
console.log(data);

// Extract USD exchange rate
const usdRate = data.conversion_rates.USD;

// Display exchange rate
const exchangeInfo = document.querySelector('#exchangeUl');
exchangeInfo.innerHTML = '';

const exchangeRateLi = document.createElement('li');
exchangeRateLi.innerText = `Exchange Rate (1 ${currencyCode} to USD): ${usdRate}`;
exchangeInfo.appendChild(exchangeRateLi);
})
.catch(err => {
console.error(`Error fetching exchange rate: ${err}`);
});
}