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

My solution for a command line stock app

It was a little difficult finding a simple stock ticker API. This was the cleanest one I could find.

// ticker-shop.js
var https = require("https");

function printMessage(ticker, price) {
  var message = ticker + ": $" + price;
  console.log(message);
}

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

function get(ticker) {
  var request = https.get("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20('" + ticker + "')%0A%09%09&env=http%3A%2F%2Fdatatables.org%2Falltables.env&format=json", function(response) {
  var body = "";
  response.on('data', function (chunk) {
    body += chunk;
  });
  response.on('end', function() {
    if(response.statusCode === 200) {
      try {
        var snapshot = JSON.parse(body);
        printMessage(snapshot.query.results.quote.symbol, snapshot.query.results.quote.Ask);
      } catch(error) {
        printError(error);
      }
    } else {
      print({message: "There was an error getting the stock price for " + ticker + ". (" + response.statusMessage + ") "});
    }
  });
  });

  request.on("error", printError);
}

module.exports.get = get;
// app.js
var snapshot = require("./ticker-shop");
var tickers = process.argv.slice(2);

tickers.forEach(snapshot .get);

Using this command:

$ node app.js JPM GS

Should yield this output:

GS: $176.89
JPM: $68.45