implemented currency selection

This commit is contained in:
0ceanSlim 2023-08-01 20:27:19 -04:00
parent 3dd177f0f3
commit c2ad2306aa
2 changed files with 25 additions and 11 deletions

View File

@ -15,7 +15,18 @@
</div>
<div id="dollars-to-sats"></div>
<div class="currency-dropdown">
<label for="currency-select">Select Currency: </label>
<select id="currency-select">
<option value="usd">Dollars (USD)</option>
<option value="jpy">Yen (JPY)</option>
<option value="eur">Euros (EUR)</option>
</select>
</div>
</div>
<script src="your_javascript_file.js"></script>
</body>
</html>

View File

@ -1,26 +1,29 @@
// Function to fetch and update the Bitcoin price
function updateBitcoinPrice() {
fetch('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd')
const currencySelect = document.getElementById('currency-select');
const selectedCurrency = currencySelect.value;
fetch(`https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=${selectedCurrency}`)
.then(response => response.json())
.then(data => {
const bitcoinPrice = data['bitcoin']['usd'];
const bitcoinPrice = data['bitcoin'][selectedCurrency];
// Update the Bitcoin price on the website
const bitcoinPriceElement = document.getElementById('bitcoin-price');
// Format the Bitcoin price with a comma after every three digits and no decimals
const formattedPrice = '$' + bitcoinPrice.toLocaleString(undefined, { minimumFractionDigits: 0, maximumFractionDigits: 0 });
const formattedPrice = `${selectedCurrency.toUpperCase()} ` + bitcoinPrice.toLocaleString(undefined, { minimumFractionDigits: 0, maximumFractionDigits: 0 });
bitcoinPriceElement.textContent = formattedPrice; // Display the formatted Bitcoin price
// Calculate dollars to sats
const dollarsToSatsElement = document.getElementById('dollars-to-sats');
// Calculate currency to sats
const currencyToSatsElement = document.getElementById('dollars-to-sats');
const satsConversionFactor = 100000000; // 1 bitcoin = 100 million sats
// Calculate how many sats you can get for 1 dollar based on the fetched Bitcoin price
const dollars = 1;
const sats = (1 / bitcoinPrice) * satsConversionFactor;
// Calculate how many sats you can get for 1 unit of the selected currency based on the fetched Bitcoin price
const currencyValue = 1;
const sats = (currencyValue / bitcoinPrice) * satsConversionFactor;
dollarsToSatsElement.textContent = `$1.00 = ${sats.toFixed(0)} sats`; // Display the result
currencyToSatsElement.textContent = `1 ${selectedCurrency.toUpperCase()} = ${sats.toFixed(0)} sats`; // Display the result
})
.catch(error => console.error(error));
}
@ -28,8 +31,8 @@ function updateBitcoinPrice() {
// Function to refresh the Bitcoin price every 30 seconds
function refreshBitcoinPrice() {
updateBitcoinPrice(); // Call the function immediately to display the price on page load
setInterval(updateBitcoinPrice, 10000); // Call the function every 30 seconds (30,000 milliseconds)
setInterval(updateBitcoinPrice, 30000); // Call the function every 30 seconds (30,000 milliseconds)
}
// Call the function to refresh the Bitcoin price
refreshBitcoinPrice();
refreshBitcoinPrice();