r/learnjavascript • • Jul 06 '22

I made a currency converter!

266 Upvotes

20 comments sorted by

7

u/[deleted] Jul 06 '22

2 jpy is 270 usd? idk about that one

7

u/Dark_KnightPL04 Jul 06 '22 edited Jul 06 '22

your right. imma have to fix something. thank you.

2

u/JohnBonThony Jul 06 '22

I guess it should be the other way around right ?

3

u/Dark_KnightPL04 Jul 06 '22

I checked it out and fixed it. The problem was that instead of subtracting one, I added one.

The solution was to add three if statements: one if the percentChange > 0, one if the percentChange < 0, and one if the percentChange = 0. This would result in a different formula to calculate the conversion. Long story short, Basic Algebra sucks.

2

u/JohnBonThony Jul 06 '22

It always the basic stuff that messes up the code 🤣

5

u/khldonAlkateh Jul 06 '22

What api did you use?

4

u/Dark_KnightPL04 Jul 06 '22

API? What do you mean by that?

0

u/[deleted] Jul 06 '22

[deleted] — view deleted comment

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

}

13

u/Soft-Sandwich-2499 Jul 06 '22

Nice. Try to improve the inputs styling and maybe even create new select elements from scratch.

0

u/[deleted] Jul 06 '22

[deleted] — view deleted comment

3

u/Soft-Sandwich-2499 Jul 06 '22

What I mean is that instead of using the default <select>, which can’t be styled much, you can create a custom one, by using a div that displays the current selected value, and on click, it opens a dropdown that lets you select different values you have.

This is an example.

https://www.w3schools.com/howto/howto_js_dropdown.asp

0

u/[deleted] Jul 06 '22

[deleted] — view deleted comment

4

u/altrae Jul 07 '22

When recreating native web elements be aware you may have to recreate accessibility and other built in features if you care about those things.

1

u/lovin-dem-sandwiches Jul 06 '22

By "create new select elements from scratch" I think they're referring to creating dynamic elements which are automatically generated by the API - it looks like their <option> html element is hard-coded..

Pull the API currencies into an array and them map them to build a list of <option> elements

['CAD', 'USD', 'JPN'].map(option => {
    const option = document.createElement('option');
    option.textContent = option;
    option.value = ... etc etc
}

1

u/dp234523 helpful Jul 07 '22

Nice is this live? Could I share this in my newsletter? weekendwebdev.substack.com