www.webdeveloper.com
Results 1 to 6 of 6

Thread: Node JS - for loops and an array

  1. #1
    Join Date
    Aug 2012
    Posts
    4

    Node JS - for loops and an array

    Hello guys,

    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...

    Thanks for reading and helping!
    Kidman

  2. #2
    Join Date
    Aug 2012
    Posts
    4
    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.

  3. #3
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    you want an object, not and array and you want to define it before the start of your for loop.

  4. #4
    Join Date
    Aug 2012
    Posts
    4
    Quote Originally Posted by rnd me View Post
    you want an object, not and array and you want to define it before the start of your for loop.
    Thank you very much for your answer. I thought an array is an object?! I'm confused now. Can you maybe elaborate a little bit what you exactly mean?

  5. #5
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    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...

  6. #6
    Join Date
    Aug 2012
    Posts
    4
    Ok I see. Thanks for your help. I will try what you said and go from there.

    Best regards
    Kidman

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles