Snap lists aren't JavaScript arrays. If you want to access Snap's data types in JS you'll need to dig into their implementation code, which is all open, but which I don't encourage as a topic on this forum.
I know this keeps coming up a lot and folks hate me for discouraging JS discussions, but hear me out: This forum is stuffed to the brim with (males) people who love Snap so much they want to hack it in the "underlying" programming language in search for ground-level truth, believing that learning how to hack Snap will make them competent JS programmers. Both motivations are problematic: If JS implementation discussions tend to spring up in the "advanced" topics discussions beginner-level learners will jump to the conclusion that learning JS somehow fast-tracks their understanding of Snap (which it doesn't). Worse, kids are going to feel peer pressure to fiddle around with mostly boring code snippets just because they get the impression that that's what the hotshots do here, and that that's something that gets then the respect of the hotshots here. And then they'll develop naive notions of how "JS" works, because they've read some code elsewhere on Stackoverflow or in Blockly that worked, but they don't realize that that wasn't "the JavaScript programming language (TM)" but some parts of a library.
In your example: "data" is a Snap list object, not an array. Snap lists have a method named length
, not a property. But because this is JavaScript, every method is also a property. Therefore, if you check whether the property (!) length
is true-ish (another problematic concept in JS, just sayin'), you believe that the result tells you whether the list has zero (false) or more (true) elements. But, since data is a Snap list it only tells you whether that objects has a property named length
whose value is not null and not undefined. Since there is a method named length
the result is always true. In order to get what you think you're getting you'd have to call length() instead. However, you're making it even worse by assigning the method (!) named "length" to the variable named "i_end" and by using that inside both a conditional and a for-loop, mistakingly assuming it's a number when it really is a function.
See, this is hard, and you didn't even learn anything, at least nothing that's even remotely interesting, or anything that will make you a better programmer. This is exactly why I strongly discourage such discussions about JS and Snap here. I know folks lap it up. They're overly eager to learn the "secrets" behind Snap. And I'm not keeping any secrets closed, by golly, it's all open. But one reason we're having it all open for everybody to see is that we don't have to talk about it here.
Now, since you're an aspiring JS programmer you've learned that slice()
copies an array. But you didn't realize that a list isn't an array and does not have a method named slice, same story. Is this really a topic that's interesting in this forum? Do you think kids - or teachers - learn something worthwhile from such a question?
It gets even worse - and way more difficult - if you want to write JavaScript functions that work on larger data sets, and that require to play ball with other scripts running simultaneously, because JavaScript doesn't support that.
Long story short, I hope you understand my point about not encouraging - and even actively discouraging - JavaScript questions and discussion here. We made Snap! so we can teach powerful ideas of programming. We did not make it so we get sucked down to haggling with the trivialities of text-based programming languages.