0

looking for a bit of javascript loops. im trying to define a bunch of coin names with for (coin in coinnames){ and use "coin" as a variable inside this loop. and i seem to be having troubles. i have tried brackets quotations and every mix in between.

var Cryptsy = require('cryptsy');
redis = require('redis');
client = redis.createClient()

var cryptsy = new Cryptsy('key', 'secret');

var bittrex = require('node.bittrex.api');
bittrex.options({
    'apikey': 'key',
    'apisecret': 'secret',
    'stream': true,
    'verbose': true,
    'cleartext': true,
    'baseUrl': 'https://bittrex.com/api/v1.1'
});


var coinnames = ['BTC', 'NEOS'];
for (coin in coinnames) {


    client.hget("Exchange_Rates", "coin", function(err7, price) {
        console.dir(coin + " Price: " + price);
        console.dir(err7)
        bittrex.sendCustomRequest('https://bittrex.com/api/v1.1/account/getbalances?apikey=key&currency=coin', function(data) {
            console.log(data.result[0].Balance);
            setTimeout(function() {
                bittrex.sendCustomRequest('https://bittrex.com/api/v1.1/market/selllimit?apikey=key&market=BTC-coin&quantity=data.result[0].Balance&rate=price', function(data) {
                    console.log(data);
                }, true);
            }, 20000);
        }, true);
    });
};
nnnnnn
  • 147,572
  • 30
  • 200
  • 241
Infernoman
  • 33
  • 8
  • Does `client.hget()` perform an asynchronous operation? – nnnnnn Mar 09 '15 at 00:09
  • i believe it does but im not positive, i am new to javascript coding so im trying to learn as i go and work out the kinks. – Infernoman Mar 09 '15 at 00:11
  • `for (coin in coinnames)` will give values of `coin` of `0` and `1`. JavaScript is not PHP, and `for ... in` loops iterate through the property **names**, not property **values**. – Pointy Mar 09 '15 at 00:11
  • Okay so doing this would be possible in PHP? or is there another way to do it in javascript by chance? – Infernoman Mar 09 '15 at 00:27

1 Answers1

0

It seems that you are not declaring the variable coin anywhere. The variable in the loop can be defined with a var:

var coinnames = ['BTC', 'NEOS'];
for (var coin in coinnames) {
    // ... use coin variable ...
}

Also note that using for..in loops for arrays is not recommended. This is because it loops over object properties, not array indices. See Why is using "for...in" with array iteration a bad idea? for details.

A better way to do this would be:

var coinnames = ['BTC', 'NEOS'];
for (var i = 0; i < coinnames.length; i++) {
    var coin = coinnames[i];
    // ... use coin variable ...
}
Community
  • 1
  • 1
metacubed
  • 7,031
  • 6
  • 36
  • 65