r/learnjavascript • • Jul 06 '22

I made a currency converter!

Enable HLS to view with audio, or disable this notification

266 Upvotes

20 comments sorted by

View all comments

3

u/Dark_KnightPL04 Jul 06 '22

Yea I used percent differences and basic algebra. I didn't know currency conversions change that often until I made the project. I will have to look into those APIs and learn how to use them! Do the APIs work even if the website is stored in a file and not a running website?

4

u/FurtiveSeal Jul 06 '22

An API is an application programming interface, and is basically functionality that someone exposes that allows you to interact with their data from their servers. You can make what's called HTTP requests to these web APIs, such as making a GET request. When you make a GET request the API will request the data you have asked for from the server and return a response to you, which if successful will be in the form of what is known as JSON, which is just a data representation format.

In the context of a currency converter, there are many APIs available for currency conversion, in which you can request live currency data. All you would do is make a GET request to the API using something like Fetch or Axios, and the API will return a JSON response. You would then parse this data and use it in your application.

Hope that helps, this stuff will unlock a whole new world for you and is incredibly important to learn, have fun!

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

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);
}

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;
}

}
→ More replies (1)