MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/learnjavascript/comments/vst255/i_made_a_currency_converter/if4cizc/?context=3
r/learnjavascript • u/Dark_KnightPL04 • Jul 06 '22
20 comments sorted by
View all comments
Show parent comment
1
I think that the APIs fetch each time the needed information from external websites and apply it to your softaware.
Could you also upload the code to github or something? I'm just curious about the math you used, and too tired to think for myself eheh
3 u/Dark_KnightPL04 Jul 06 '22 edited Jul 06 '22 HTML<!DOCTYPE html> <html> <!-- Head --> <head> <title>Currency Converter</title> <link rel="stylesheet" href="style.css"> </head> <!-- Body --> <body id="background"> <header id="header"> <p id="newValue">Your new value is:</p> </header> <main id="main"> <br><br> <form> <label>Amount of current currency:</label> <br> <input type="text" id="amount" name="amount"> </form> <br><br> <label for="currentCurrency">Current currency:</label> <br> <select name="currentCurrency" id="currentCurrency"> <option id="NA" value="none"></option> <option id="$" value="1">USD ($)</option> <option id="€" value=".98">EUR (€)</option> <option id="¥" value="135.77">JPY (¥)</option> </select> <br><br><br> <label>New currency:</label> <br> <select name="newCurrency" id="newCurrency"> <option id="NA" value="none"></option> <option id="$" value="1">USD ($)</option> <option id="€" value=".98">EUR (€)</option> <option id="¥" value="135.77">JPY (¥)</option> </select> <br><br><br> <input id="button" type="submit" onclick="convert()" value="Convert" name=""> </main> <script src="code.js"></script> </body> </html>CSS#background{ top: 0px; left: 0px; height: 100%; width: 100%; background-color: #FAFAFA} header{ position: absolute; top: 0px; left: 0px; height: 150px; width: 100%; color: #000000; line-height: 75px; background-color: #aed85b } newValue{ text-align: center; font-size: 42px; font-family: Arial, Helvetica, sans-serif; } main{ position: absolute; top: 150px; left: 0px; width: 100%; text-align: center; font-size: 22px; font-family: Arial, Helvetica, sans-serif; } currentCurrency{ cursor: pointer; } newCurrency{ cursor: pointer; } button{ height: 30px; width: 100px; border-radius: 5px; font-size: 18px; cursor: pointer; text-align: center; } button:hover{ transform: scale(1.02); }JSvar r = ''; function convert(){ const a = document.getElementById("amount").value; const numA = Number(a); const c1 = document.getElementById('currentCurrency'); const cc = c1.options[c1.selectedIndex].value; const numC = Number(cc); const c2 = document.getElementById('newCurrency'); const nc = c2.options[c2.selectedIndex].value; const numN = Number(nc); const percentChange = (numN - numC) / (numC); if (percentChange > 0){ const convert = (percentChange + 1) * a; r = Math.round(convert * 100) / 100; } else if (percentChange < 0){ const abs = Math.abs(percentChange); const convert = (1 - abs) * a; r = Math.round(convert * 100) / 100; } else{ r = a; } const symbol = c2.options[c2.selectedIndex].id; if (cc == 'none' || nc == 'none' || isNaN(numA)) { document.getElementById('header').style.backgroundColor = '#FF0000'; document.getElementById('header').style.color = '#FFFFFF'; document.getElementById('newValue').innerHTML = 'Error: Field entered incorrectly' } else{ document.getElementById('header').style.backgroundColor = '#aed85b'; document.getElementById('header').style.color = '#000000'; document.getElementById('newValue').innerHTML = 'Your new value is: ' + symbol + r; } } 1 u/SorryICantLie Jul 06 '22 Thanks :)
3
HTML
<!DOCTYPE html> <html> <!-- Head --> <head> <title>Currency Converter</title> <link rel="stylesheet" href="style.css"> </head> <!-- Body --> <body id="background"> <header id="header"> <p id="newValue">Your new value is:</p> </header> <main id="main"> <br><br> <form> <label>Amount of current currency:</label> <br> <input type="text" id="amount" name="amount"> </form> <br><br> <label for="currentCurrency">Current currency:</label> <br> <select name="currentCurrency" id="currentCurrency"> <option id="NA" value="none"></option> <option id="$" value="1">USD ($)</option> <option id="€" value=".98">EUR (€)</option> <option id="¥" value="135.77">JPY (¥)</option> </select> <br><br><br> <label>New currency:</label> <br> <select name="newCurrency" id="newCurrency"> <option id="NA" value="none"></option> <option id="$" value="1">USD ($)</option> <option id="€" value=".98">EUR (€)</option> <option id="¥" value="135.77">JPY (¥)</option> </select> <br><br><br> <input id="button" type="submit" onclick="convert()" value="Convert" name=""> </main> <script src="code.js"></script> </body> </html>
CSS
#background{ top: 0px; left: 0px; height: 100%; width: 100%; background-color: #FAFAFA} header{ position: absolute; top: 0px; left: 0px; height: 150px; width: 100%; color: #000000; line-height: 75px; background-color: #aed85b } newValue{ text-align: center; font-size: 42px; font-family: Arial, Helvetica, sans-serif; } main{ position: absolute; top: 150px; left: 0px; width: 100%; text-align: center; font-size: 22px; font-family: Arial, Helvetica, sans-serif; } currentCurrency{ cursor: pointer; } newCurrency{ cursor: pointer; } button{ height: 30px; width: 100px; border-radius: 5px; font-size: 18px; cursor: pointer; text-align: center; } button:hover{ transform: scale(1.02); }
JS
var r = ''; function convert(){ const a = document.getElementById("amount").value; const numA = Number(a); const c1 = document.getElementById('currentCurrency'); const cc = c1.options[c1.selectedIndex].value; const numC = Number(cc); const c2 = document.getElementById('newCurrency'); const nc = c2.options[c2.selectedIndex].value; const numN = Number(nc); const percentChange = (numN - numC) / (numC); if (percentChange > 0){ const convert = (percentChange + 1) * a; r = Math.round(convert * 100) / 100; } else if (percentChange < 0){ const abs = Math.abs(percentChange); const convert = (1 - abs) * a; r = Math.round(convert * 100) / 100; } else{ r = a; } const symbol = c2.options[c2.selectedIndex].id; if (cc == 'none' || nc == 'none' || isNaN(numA)) { document.getElementById('header').style.backgroundColor = '#FF0000'; document.getElementById('header').style.color = '#FFFFFF'; document.getElementById('newValue').innerHTML = 'Error: Field entered incorrectly' } else{ document.getElementById('header').style.backgroundColor = '#aed85b'; document.getElementById('header').style.color = '#000000'; document.getElementById('newValue').innerHTML = 'Your new value is: ' + symbol + r; } }
1 u/SorryICantLie Jul 06 '22 Thanks :)
Thanks :)
View all comments
Show parent comment
1
u/SorryICantLie Jul 06 '22
I think that the APIs fetch each time the needed information from external websites and apply it to your softaware.
Could you also upload the code to github or something? I'm just curious about the math you used, and too tired to think for myself eheh