Wrong .length value?


#1

I can see 3 friends in a friend-array, but when i write friends.length it shows that the value is 39. Care to explain why this is the case?


#2

Sorry about that! It’s a bug–the serialized representation of friends as a string has length 39. I’m going to be working on the bugs with the hover debugging soon.


#3

Until this bug is fixed you could use what is essentially a foreach loop in javascript.

for (var i in arrayItems) {
var item = arrayItems[i];
}

Assume arrayItems contains these items [Thang1, Thang2, Thang3, Thang4, Thang5]. Variable i will act as a counter like you use in a normal for loop and will automatically increment every loop. Following that same logic we use it to apply the current value in arrayItems (matching the index) to variable item (first iteration assigns Thing1, second iteration assigns Thing2 and so on until it completes the array like you do in a for loop) and can then proceed with what ever actions/code we wan for said item. If you need any clarification just let me know and I will try to explain it better.


#4

Ah! I’m not used to that kind of for-each loop (or javascript at all). Tried with for(Obj x : array) but that didn’t work. I guess it was a matter of syntax then, in addition to that bug.


#5

The normal for-loop syntax should work in this level, too, despite the misleading hover length display. I’ve updated the for-loop snippet documentation to include a better example. I’ll also include the example here.

var friends = this.getFriends();
for(var i = 0; i < friends.length;  ++i) {
    var friend = friends[i];
    this.say('I see you, ' + friend.id);
}