Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,319 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;