12. Currency Converter - JSBook - Simple Calculations
Здравейте,
имам проблем с тази задача
получавам 80/100
https://judge.softuni.bg/Contests/Practice/Index/927#11
https://judge.softuni.bg/Contests/Submissions/View/15200762
function convertCurrency([amount, entryNotes, exitNotes]) {
amount = Number(amount);
if (entryNotes === "BGN" && exitNotes === "BGN") {
console.log(Number(amount.toFixed(2)));
} else if(entryNotes !== "BGN" && exitNotes === "BGN") {
currencyToBgn([amount, entryNotes]);
} else if (entryNotes === "BGN" && exitNotes !== "BGN") {
let result = bgnToCurrency([amount, exitNotes]);
console.log(Number(result.toFixed(2)));
} else if (entryNotes !== "BGN" && exitNotes !== "BGN") {
currencyToCurrency([amount, entryNotes, exitNotes]);
}
function currencyToBgn([amount, entry]) { // if exit === BGN
let convert;
let course;
if(entry === "USD") {
course = 1.79549;
} else if(entry === "EUR") {
course = 1.95583;
} else if(entry === "GBP") {
course = 2.53405;
}
amount = Number(amount);
course = Number(course);
convert = amount * course;
console.log(Number(convert.toFixed(2)));
}
function bgnToCurrency([amount, exit]) { // if entry === BGN
amount = Number(amount);
let convert;
let course;
if(exit === "USD") {
course = 1.79549;
} else if(exit === "EUR") {
course = 1.95583;
} else if(exit === "GBP") {
course = 2.53405;
}
amount = Number(amount);
course = Number(course);
convert = amount / course;
convert = Number(convert.toFixed(2));
return convert;
}
function currencyToCurrency([amount, entry, exit]) {
amount = Number(amount);
let bgnToEntry = bgnToCurrency([amount, entry]);
let bgnToExit = bgnToCurrency([amount, exit]);
let result = amount / (bgnToEntry / bgnToExit);
result = Number(result);
console.log(Number(result.toFixed(2)));
}
}
Ето още един вариант!
function curreConvertMoney([arg1,arg2,arg3]){
let currValue = Number(arg1);
let fromCurrency = arg2;
let toCurrency = arg3;
switch(fromCurrency){
case "USD":
currValue *= 1.79549;
break;
case "EURO":
currValue *= 1.95583;
break;
case "GBP":
currValue *= 2.53405;
break;
default:
break;
}
switch(toCurrency){
case "USD":
currValue /= 1.79549;
break;
case "EURO":
currValue /= 1.95583;
break;
case "GBP":
currValue /= 2.53405;
break;
default:
break;
}
console.log(currValue.toFixed(2));
}
curreConvertMoney(["20", "USD", "BGN"]);
curreConvertMoney(["100", "BGN", "EURO"]);
curreConvertMoney(["12.35", "EURO", "GBP"]);
curreConvertMoney(["150.35", "USD", "EURO"]);