I hope it's ok to post here since I'm using node js but in the end it is JavaScript (on the server). I'm completely new to JavaScript and programming in general and I'm sure the problem of mine is trivial to all of you so don't be mad at me for asking here .
My problem:
I'm trying to write an automatic betting software. A bot that connects to the betfair servers and places bets independently. For that I am using Node Js and a betfair-sports-api library. At the moment I am writing a function which grabs all the horse racing markets today and "filters" them according to part of their market name. What I would like to do is write the marketId of each market that went through my filter in an array so that I could later print the marketId of the first market in that array for example. What I did so far:
Code:
function markets(cb) {
console.log("=======Getting Markets=======");
// eventTypeIds 1-soccer, 2-tennis
var inv = session.getAllMarkets({
eventTypeIds : [ 7 ], countries : [ 'GBR' ], fromDate : "2012-08-11T01:00:00", toDate : "2012-08-11T23:00:00"
});
inv.execute(function(err, res) {
if (err)
console.log("Error in getAllMarkets" +err);
else
for(var index in res.result.marketData) {
market = res.result.marketData[index];
var marketName = res.result.marketData[index].marketName;
var char = marketName.charAt(0);
if (isNaN(char))
continue;
var array = [];
array[index] = res.result.marketData.marketId;
console.log(market);
}
marketId = array[0]
console.log('Gewählte ID= %s', marketId) ;
cb(null, "OK");
});
}
This does not work... it only writes the last grabbed market into the array...
Ok I edited the code a bit and now it partly works. New code:
Code:
function markets(cb) {
console.log("=======Getting Markets=======");
// eventTypeIds 1-soccer, 2-tennis
var inv = session.getAllMarkets({
eventTypeIds : [ 7 ], countries : [ 'GBR' ], fromDate : "2012-08-12T01:00:00", toDate : "2012-08-12T23:00:00"
});
inv.execute(function(err, res) {
if (err)
console.log("Error in getAllMarkets" +err);
else
for(var index in res.result.marketData) {
market = res.result.marketData[index];
var marketName = market.marketName;
var char = marketName.charAt(0);
if (isNaN(char))
continue;
console.log(market);
array [index] = market.marketId;
}
marketId = array[1]
lang = array.length;
console.log(array);
console.log(lang);
console.log('Gewählte ID= %s', marketId) ;
cb(null, "OK");
});
}
The problem now is that I get back 14 markets but have 27 elements in my array... I have the first marketId then an empty space then the second id and so on. I don't understand this. The continue break should start the loop with the next value why is the array command still being executed?
Sorry for the double post... I didn't see an edit button.
you define array just before you set one value on it; index.
arrays ([]) are supposed to use numbered index, while objects use string index.
confusingly, array can use strings, but when you loop it by length, those keys won't show in an array...
define the array out more, so you don't destroy just before each time you set the one index property...
Bookmarks