This repository has been archived by the owner on Apr 27, 2021. It is now read-only.
forked from hfgcoding/01_01_currency_reloaded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
92 lines (72 loc) · 2.85 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//import { LOADIPHLPAPI } from "dns";
/*
* CURRENCY CONVERTER RELOADED
* Author: <your name here>
* ---------------------------
*
* This converts currencies...somehow.
*
* A list of ressources you used, for example links:
* [JavaScript Reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference).
*/
/*
* Aufgabe: Baut einen neuen Währungsumrechner. Nachfolgend findet ihr Code der die
* dafür notwendingen Eingabewerte von der Konsole entgegennimmt.
*
* Dafür müsst ihr das Script wie folgt aufrufen:
* npm start -- <Ausgangssumme> <Ausgangswährung-Code> <Zielwährung-Code>
* also z.B.
* npm start -- 10.0 USD EUR
*
* Die erwartete Ausgabe ist ein Text in folgender Form:
* "Ergebnis: <Ausgangssumme> <Ausgangswährung> = <Ergebnis> <Zielwährung>"
* also z.B.
* Ergebnis: 10.00 USD = 11.00 EUR
*
* Das Script soll mindestens drei verschiedene Währungen in beide Richtungen unterstützen
*/
let args = process.argv.slice(2);
// Eingabe-Variablen deklarieren
let amount, originalCurrency, targetCurrency;
// Konsolen-Argumente in Variablen speichern
if (args.length < 3) {
console.log('Error: Not enough input arguments given!');
} else {
amount = args[0];
originalCurrency = args[1];
targetCurrency = args[2];
}
// Variable 'output' deklarieren
// let output;
// Objekt Währungen mit Wechselkurs und Symbolen deklarieren
const currencies = {EUR:{rate:1,symbol:'€'},USD:{rate:1.11,symbol:'US$'},CZK:{rate:25.58,symbol:'Kč'},BWP:{rate:12.04,symbol:'P'},AUD:{rate:1.63,symbol:'AU$'},CNY:{rate:7.78,symbol:'RMB¥'},TRY:{rate:6.34,symbol:'₺'},ZAR:{rate:16.33,symbol:'R'}};
let download;
const request = require('request');
request('https://api.exchangeratesapi.io/latest', function (error, response, body) {
//console.error('error:', error); // Print the error if one occurred
//console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
//console.log('body:', body); // Print the HTML for the Google homepage.
download = JSON.parse(body);
try {
for (const pos in download.rates) {
if (currencies.hasOwnProperty(pos)) {
//const element = download.rates[rates];
currencies[pos].rate = download.rates[pos];
} else if (currencies.hasOwnProperty(pos) === false) {
currencies[pos] = {};
currencies[pos].rate = download.rates[pos];
}
}
} catch (error) {
console.error(error);
}
const amountInEur = amount / currencies[originalCurrency].rate;
const output = amountInEur * currencies[targetCurrency].rate;
// Ausgabe 'output'
if (currencies[targetCurrency].hasOwnProperty('symbol')) {
console.log(`Das Ergebnis ist: ${output}${currencies[targetCurrency].symbol}`);
} else {
console.log(`Das Ergebnis ist: ${output} ${targetCurrency}`);
}
//console.log(currencies);
});