martio
martio

Reputation: 121

How to convert JavaScript to CoffeeScript for two case

I'm using CoffeeScript in a Rails application. How to convert JavaScript to CoffeeScript for two case:

var colIndex = 0,
    colRight = 0,
    cols = this.grid.columnX,
    len = cols.length,
    cmatch = false;

for (len; colIndex < len; colIndex++) {
    colRight = cols[colIndex].x + cols[colIndex].w;
    if (xy[0] < colRight) {
        cmatch = true;
    break;
    }
}

and

setTimeout(function() {
    d.scrollTop = st;
}, 10);

Thank you in advance for your help!

Upvotes: 0

Views: 203

Answers (3)

Chris Subagio
Chris Subagio

Reputation: 6329

Here's my stab at it:

for col, idx in @grid.columnX when xy[0] < colRight = (col.x + col.w)
  cmatch = idx
  break

So after the loop, cmatch will either be undefined or the index of the match, while colRight will be the matching col's right side, or the last col's right side if no match is found.

Here's a fiddle to play in: http://jsfiddle.net/fNSXE/1/

Upvotes: 0

island205
island205

Reputation: 1740

there is a site to do this work js2coffee:

the answers are:

1

colIndex = 0
colRight = 0
cols = @grid.columnX
len = cols.length
cmatch = false
len
while colIndex < len
  colRight = cols[colIndex].x + cols[colIndex].w
  if xy[0] < colRight
    cmatch = true
    break
  colIndex++

2

setTimeout (->
  d.scrollTop = st
), 10

Upvotes: 0

qiao
qiao

Reputation: 18219

1.CoffeeScript supports for in iteration on an array, so you simply don't need colIndex and len.

colRight = 0
cols = @grid.columnX
cmatch = false

for col in cols
  colRight = col.x + col.w
  if xy[0] < colRight
    cmatch = true
    break

2.

setTimeout (-> d.scrollTop = st), 10

Upvotes: 1

Related Questions