Skip to content

Commit 224362b

Browse files
committed
[HW-7.11/st-compl] currency-conversion
Organization of currency conversion logic/function (set of conditions). Worth noting: - number of "if...else" blocks. FS-dev: B-3 / JS basic
1 parent 53e6f50 commit 224362b

File tree

1 file changed

+70
-0
lines changed
  • full-stack-dev/3-js-basic/7-functions/7-11-hw-4-currency-conversion

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Нужно организовать логику/функцию для конвертации валют (нескольких пар), согласно входных параметров:
2+
// - сумма средств, валюта (входящих) средств, целевая валюта (в какую конвертировать).
3+
// - если такой (целевой) валюты нет, возврат - null.
4+
// - поддерживаемые валюты (например, рубли, доллары, евро).
5+
6+
const CONVERSION_RU_USD = 79.55;
7+
const CONVERSION_RU_EUR = 93.35;
8+
const CONVERSION_USD_EUR = 0.92;
9+
10+
function getConversion(currencyAmount, inCurrency, outCurrency) {
11+
if (!currencyAmount || !inCurrency || !outCurrency) {
12+
return null;
13+
}
14+
15+
if (typeof currencyAmount !== 'number') {
16+
return null;
17+
} else if (
18+
typeof inCurrency !== 'string' ||
19+
typeof outCurrency !== 'string'
20+
) {
21+
return null;
22+
}
23+
24+
const inCurr = inCurrency.toLowerCase();
25+
const outCurr = outCurrency.toLowerCase();
26+
let convertSum;
27+
28+
if (inCurr !== 'rub' && inCurr !== 'usd' && inCurr !== 'eur') {
29+
return null;
30+
} else if (outCurr !== 'rub' && outCurr !== 'usd' && outCurr !== 'eur') {
31+
return null;
32+
}
33+
34+
if (inCurr === outCurr) {
35+
return null;
36+
}
37+
38+
if (inCurr === 'rub' && outCurr === 'usd') {
39+
return Number((convertSum = currencyAmount / CONVERSION_RU_USD).toFixed(2));
40+
} else if (inCurr === 'usd' && outCurr === 'rub') {
41+
return Number((convertSum = currencyAmount * CONVERSION_RU_USD).toFixed(2));
42+
}
43+
44+
if (inCurr === 'rub' && outCurr === 'eur') {
45+
return Number((convertSum = currencyAmount / CONVERSION_RU_EUR).toFixed(2));
46+
} else if (inCurr === 'eur' && outCurr === 'rub') {
47+
return Number((convertSum = currencyAmount * CONVERSION_RU_EUR).toFixed(2));
48+
}
49+
50+
if (inCurr === 'usd' && outCurr === 'eur') {
51+
return Number(
52+
(convertSum = currencyAmount * CONVERSION_USD_EUR).toFixed(2)
53+
);
54+
} else if (inCurr === 'eur' && outCurr === 'usd') {
55+
return Number(
56+
(convertSum = currencyAmount / CONVERSION_USD_EUR).toFixed(2)
57+
);
58+
}
59+
}
60+
61+
console.log(getConversion(1000, 'RUB', 'USD')); // 12.57
62+
console.log(getConversion(100, 'USD', 'RUB')); // 7955
63+
console.log(getConversion(5000, 'RUB', 'EUR')); // 53.56
64+
console.log(getConversion(500, 'EUR', 'RUB')); // 46675
65+
console.log(getConversion(50, 'USD', 'EUR')); // 46
66+
console.log(getConversion()); // null
67+
console.log(getConversion(10, 'rub', 'RUB')); // null
68+
console.log(getConversion(10, 'RUB', 'BLR')); // null
69+
console.log(getConversion('10', 'eur', 'usd')); // null
70+
console.log(getConversion(10, '123', 10)); // null

0 commit comments

Comments
 (0)