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

Michael Timbs
Michael Timbs
15,761 Points

Code efficiency

Can someone please suggest any ways to make this code a little more efficient? Or point out better ways to do something.... this is the first thing I have tried to build with javascript (or any scripting language).

Michael Timbs
Michael Timbs
15,761 Points
//******************************
// Set up a looping function
//*****************************
var Abstraction = function() {
  this.index = -1;
};

Abstraction.prototype.getIndex = function getIndex() {
  this.index++;
  return this.index;
};

Abstraction.prototype.isDoneTest = function isDoneTest() {
  return this.index > 6;
};

var list = new Abstraction();
//******************************
// End of looping stuff.
//*****************************




var https = require("https");
var crownURL = "https://api.beteasy.com.au/Event/QueryMasterEventsByClassAndOrderBy?OnlyIsOpenForBetting=true&DateFrom=2015-03-04T04%3A55%3A00.0000000%2B00%3A00&DateTo=2016-03-04T12%3A59%3A00.0000000%2B00%3A00&MasterEventTypeID=2&EventTypes=101&MasterCategoryID=16&CategoryClassOrderBys=0-99&EventClassOrderByMode=1&EventClassOrderByValue=1&RowsPerPage=50&CurrentPage=0";

//Connect to the API URL 


var request = https.get( crownURL, function(response) {
    //console.log("statusCode: ", response.statusCode);
    var body = "";
    //READ THE DATA
    response.on('data', function(chunk){
        body += chunk;
    });
    response.on('end', function(){
        //PARSE DATA
        var markets = JSON.parse(body);
        //PRINT DATA 
        //Print the match name
        function iterator(){
          var i = list.getIndex();

        var gameTwo = (markets.Results[i].MasterEventName);
        var homeTeamTwo = (markets.Results[i].Events[1].Outcomes[0].OutcomeName);
        var homeH2HOdds = (markets.Results[i].Events[0].FixedMarkets[0].Price);
        var awayTeam = (markets.Results[i].Events[1].Outcomes[1].OutcomeName);
        var awayH2HOdds = (markets.Results[i].Events[0].FixedMarkets[1].Price);
        var homeLine = (markets.Results[i].Events[1].FixedMarkets[0].Points);
        var awayLine = (markets.Results[i].Events[1].FixedMarkets[1].Points);

        console.log(gameTwo);
        console.log("Head to Head");
        console.log(homeTeamTwo, homeH2HOdds);
        console.log(awayTeam, awayH2HOdds.toFixed(2));
        console.log("Line");
        console.log(homeTeamTwo, homeLine, "$1.92");
        console.log(awayTeam, awayLine, "$1.92");
        console.log("");

          if(list.isDoneTest()){
            clearInterval(interval);
          }
        }
        var interval = setInterval(iterator,1);
    });
});

request.on("error", function(error){
    console.error(error.message);
});

1 Answer

Michael Timbs
Michael Timbs
15,761 Points

Thanks for that! looks a lot neater splitting it like that