Baffled by [i]

54 views
Skip to first unread message

Larry Serflaten

unread,
Jan 15, 2016, 12:34:16 AM1/15/16
to Khan Academy Javascript Technical Q&A
I ran into a program from a novice using a strange syntax that apparently was acceptable.
The program was printing text inside a loop, but used the loop iterator enclosed in brackets
to position the text, such as:

text(list[i], [i], 20);

And oddly enough it works, as in:


var txt = ["A", "B", "C", "D", "E"];
fill(0);
for (var i=0; i<txt.length; i++){
    text(txt[i], 20, [i]);              // what is [i]  ???
    text(i + "  " + [i], 40, [i]);      // clearly not = i,  or is it?
   
}
textAlign(LEFT, TOP);     // something to do with text() ?
for (var j=txt.length; j--;){
    text(txt[j], 200, [j]);
    text(j + "  " + [j], 240, [j]);
}


The output should all be at Y locations of 1-4, but its not.
In the first loop, the text is on a separate line, and in the
second loop, the text is like on separate paragraphs.

I can find no documentation that explains this, does someone have
some insight as to what is going on?

Thanks
LFS
Message has been deleted

Element118

unread,
Jan 16, 2016, 10:44:39 AM1/16/16
to Khan Academy Javascript Technical Q&A
[i] constructs an array with one element. In the text function, I guess the [i] in text(txt[i], 20, [i]); would be converted into a number, and the [i].toString() will be called for i + " " + [i]. The code suggests that the text overlaps with itself.

Larry Serflaten

unread,
Jan 17, 2016, 5:47:56 AM1/17/16
to Khan Academy Javascript Technical Q&A


On Saturday, January 16, 2016 at 9:44:39 AM UTC-6, Element118 wrote:
[i] constructs an array with one element.
... 
The code suggests that the text overlaps with itself.


You should try the code and see the results.  I also would expect the text to
overlap itself, but it does not, and that is what is surprising.

Here is the loop:

for (var i=0; i<txt.length; i++){
    text(txt[i], 20, [i]);
}

If you were to unwrap the loop and put in literal values, then it should
unwrap like:

text("A", 20, 0);
text("B", 20, 1);
text("C", 20, 2);
text("D", 20, 3);

That is what I would expect, where the text should overlap.  But the
result looks more like:

text("A", 20, 0);
text("B", 20, 12);
text("C", 20, 24);
text("D", 20, 36);

If you print [i] to try to see where they get their value, it matches the
iterator i, so why doesn't the text overlap?  It is still baffling me....

LFS
 

Element118

unread,
Jan 18, 2016, 1:39:01 AM1/18/16
to Khan Academy Javascript Technical Q&A
The same behaviour happens replacing [i] with ""+i. The value of TOP is 101 and the spacing of the text is 100, but I cannot find similar patterns...
Message has been deleted

Bob Lyon

unread,
Jan 5, 2018, 10:03:32 PM1/5/18
to Khan Academy Javascript Technical Q&A
If you look at the implementation of `text`, it splits its first argument into separate lines and invokes the internal function `text$4(lines[count], x, y+yOffset);` In your first loop `yOffset` is inevitably zero, so the value of the last argument to `text$4` is a string that appears to be the user's intent times ten, e.g. '00', '10', '20', '30', '40'.  `text$4` invokes another internal function after manipulating the its arguments -- something like `textInternal(t, x, y-yOnset);` In your first loop, the value of `yOnset` is inevitably zero.  Javascript knows that the operands of subtraction are numbers so it coerces the `y` argument to a number, e.g. the number zero, ten, twenty, thirty, and forty.  And everybody is happy and confused.
Reply all
Reply to author
Forward
0 new messages