Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

Need input

Hi,

I'm trying to solve the currency converter challenge given by Andrew Chalkley after the last course in Node.js Basics, and it's "working" for now with some minor bugs (You need to enter the currency in all uppercase for some odd reason).

I could use some feedback/input for what to add, fix or improve.

Console input: node app.js 50 DKK NOK

Console output: 50 DKK = 62 NOK

app.js

const currency = require('./currency');

const value = process.argv.slice(2);
const fromCurrency = process.argv.slice(3)[0];
const toCurrency = process.argv.slice(4);

currency.get(value, fromCurrency, toCurrency);

currency.js

const https = require('https');

function printError(error) {
    console.error(error.message);
}

function get(value, fromCurrency, toCurrency) {
    try {
        const request = https.get(`https://api.fixer.io/latest?base=${fromCurrency}`, res => {
            let body = "";
            res.on('data', data => {
                body += data;
            });

            res.on('end', () => {
                try {
                    const amount = parseInt(value);
                    const currency = JSON.parse(body);
                    console.log(`${amount} ${currency.base} = ${Math.round(amount * currency.rates[toCurrency])} ${toCurrency}`);
                } catch(err) {
                    printError(err);
                }
            });
        });

        request.on('error', err => {
            printError(err);
        });
    } catch(err) {
        printError(err);
    }
}

module.exports.get = get;