Професионална програма
Loading...
+ Нов въпрос
ihalkov avatar ihalkov 2 Точки

12. Currency Converter - JSBook - Simple Calculations

Здравейте,

имам проблем с тази задача 

https://js-book.softuni.bg/chapter-02-simple-calculations.html#%D0%B7%D0%B0%D0%B4%D0%B0%D1%87%D0%B0------%D0%BC%D0%B5%D0%B6%D0%B4%D1%83%D0%B2%D0%B0%D0%BB%D1%83%D1%82%D0%B5%D0%BD-%D0%BA%D0%BE%D0%BD%D0%B2%D0%B5%D1%80%D1%82%D0%BE%D1%80

получавам 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)));
}
}

12. Currency Converter - JSBook - Simple Calculations

12. Currency Converter - JSBook - Simple Calculations

0
Programming Basics 08/04/2020 00:38:23
ihalkov avatar ihalkov 2 Точки

Здравейте,

Реших си задачата проблема беше че на някой от тестовете резултата ми беше без 2 знака след десетичната запетая.

Пр: ако amount = 100;

console.log(Number(amount.toFixed(2))); // тук ми беше грешката, като се принтира даваше пак 100

// Вярното е 

console.log(amount.toFixed(2)); // тогава се принтира 100.00

Също съм добавил ако въведената стойност е 0 (нула), да се принтира 0.00

Прилагам вярното решение + тестове, може да е от полза на някого

function convertCurrency([amount, entryNotes, exitNotes]) {
    amount = Number(amount);

    if (amount === 0) {
        console.log(amount.toFixed(2));
    }  else if (entryNotes === "BGN" && exitNotes === "BGN") {
        console.log(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(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(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;
    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);
    console.log(result.toFixed(2));
}
}

// Tests
convertCurrency(["100", "BGN", "BGN"]);
convertCurrency(["100", "BGN", "USD"]);
convertCurrency(["100", "BGN", "EUR"]);
convertCurrency(["100", "BGN", "GBP"]);
console.log();
convertCurrency(["100", "EUR", "EUR"]);
convertCurrency(["100", "EUR", "BGN"]);
convertCurrency(["100", "EUR", "USD"]);
convertCurrency(["100", "EUR", "GBP"]);
console.log();
convertCurrency(["100", "USD", "BGN"]);
convertCurrency(["100", "USD", "EUR"]);
convertCurrency(["100", "USD", "GBP"]);
convertCurrency(["100", "USD", "USD"]);
console.log();
convertCurrency(["100", "GBP", "BGN"]);
convertCurrency(["100", "GBP", "EUR"]);
convertCurrency(["100", "GBP", "USD"]);
convertCurrency(["100", "GBP", "GBP"]);

convertCurrency(["0", "GBP", "GBP"]);

 

0
NikolaiNikolaev avatar NikolaiNikolaev 1 Точки

Ето още един вариант!

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"]);

0