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

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