javascriptlivescript

LiveScript for loop not behaving or I have the wrong syntax


Under normal circumstances (vanilla JS) I might do something like this -

var mystring = "foo";
for(i = 0; i < mystring.length; i++) {
    console.log(i);
}

Which will return [0,1,2]`

I cannot find the syntax to produce that behavior in LiveScript. The closest I have come is this -

 mystring = \foo
 for i from 0 to my.length-1 // note the -1
     console.log i

which compiles to this JavaScript -

var mystring, i$, to$, i;
mystring = 'foo';
for (i$ = 0, to$ = mystring.length - 1; i$ <= to$; ++i$) {
  i = i$;
  console.log(i);
}

This also returns [0,1,2].

If I do not include the -1 the array returned is [0,1,2,3] which is expected because of how LiveScript compiles to JavaScript in this case.

Is it not possible to get a pure 'less than' condition in LiveScript?


Solution

  • You want to use til, not to

    for i from 0 til my.length
    

    LiveScript loops