1

I'm working through the fabously-named learnyounode. #9 is "JUGGLING ASYNC" and the instructions are to take in 3 urls as params and output the content in parameter order. I got it working by including a counter, but my original did not work:

var http = require('http'),
  bl = require('bl'),
  store = [],
  count = 0; //added later

process.argv.forEach(function(element, index, array) {
  if (index > 1) {
    http.get(element, function(response) {
      response.pipe(bl(function(err, data) {
        store[index] = data.toString();
        count++; //added later
        if (store.length == 3) {
            store.forEach(function(e,is,a) {
                console.log(e);
            });
        }
      }));
    });
  }
});

Now the thing works fine if you replace store.length with count on line 12, I just can't figure out why .length on the array wasn't enough. Anyone know?

4
  • I acknowledge a for loop instead of process.argv.forEach would have saved 2 loops. Commented Dec 17, 2014 at 21:28
  • 2
    I cannot tell what your question is. What was your code originally, what did you expect, and what did you get instead? Commented Dec 17, 2014 at 21:29
  • 1
    I have absolutely no idea what you're asking.. Commented Dec 17, 2014 at 21:31
  • store.length can give odd responses; index should change consistently in the forEach loop. Try index ... Commented Dec 17, 2014 at 21:31

2 Answers 2

4

In javascript arrays, when you give an empty array an item at index 5, it implies that the array is 6 items long, with items 0-4 being undefined.

For example

var x = [];
console.log(x.length); //prints 0;
x[2] = "hello, world";
console.log(x.length); //prints 3;

You're skipping the first two elements but still using index, so when you enter your first element into store, you're entering it into store[2], making the length 3, when count is only 1.

Sign up to request clarification or add additional context in comments.

4 Comments

Ah, this was a surprise to me... (but perhaps it shouldn't) var x=[]; x["a"]=1; console.log(x.length); // prints 0
Yeah javascript has a pretty loose interpretation of objects. I just used something in a loop the other day like x[otherArray.indexOf(item)] = item. If it can't find it in the other array it goes into -1, which doesn't count towards length so essentially gets ignored.
Playing around with it further, I started over with: var x=[]; x['a']='a'; console.log(x); // yields [ a: 'a' ] console.log(x.length) // again, 0 x[1]=1; console.log(x); // yields [ , 1, a: 'a' ] console.log(x.length); // yields 2 for the empty 0 place and the 1 place ...anyway, I though that was interesting. It's almost like these alpha indexed array items are actually object items, and thus don't get counted. That's probably a silly way of thinking of it...
The Mozilla Developer Network (MDN) has some really great documentation on javascript and HTML. It might help to read up on Arrays (there's also a specific section for the relationship between length and numerical properties). A [] is actually a simple object that is an instance of the Array class, with many functions and properties.
0

You should use your count variable, because:

When you you do store[1] = data.toString();, for example, you get an array [undefined, 1] with length = 2. And you want it to be 1.

5 Comments

No. The instructions are clear: output the results in parameter order.
If you push it to the end of the array, you'll have an indeterministic order.
@Bergi, it's an array, not an object. Arrays always keep the order. Regular objects - don't.
@Bergi, or could you please give me a link where I can read about it, if I'm mistaken.
I'm not saying the array is forgetting the order, I say that you are appending them in the possibly wrong order. Notice that http.get and bl are asynchronous.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.