0

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!

1

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

The setTimeout looks fine, but you'd overlooking that the OP might have actually wanted to have the index of the matching item. See my answer for a solution that provides that.
0

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

Comments

0

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.