1

Keeps saying that there is an unexpected if, but nothing I change is accepted.

addAnimation : (anim) ->
    if anim.time > @animations[@animations.length - 1]
        @animations.push(anim)
    else if anim.time < @animations[0]
        @animations.unshift(anim)
    else
        for index in [[email protected]]
            if anim.time > @animations[index-1].time
            and anim.time < @animations[index]
                @animations.splice(index, 0, anim)
                    break

All the spacing is as correct as possible. I use 4 for all indentation and there is no trailing whitespace. Have I misunderstood something about the syntax. Btw, this is supposed to be a simple insertion sort.

1 Answer 1

2

If you put the and inline and remove the extra indentation on the break statement then it works fine.

addAnimation : (anim) ->
    if anim.time > @animations[@animations.length - 1]
        @animations.push(anim)
    else if anim.time < @animations[0]
        @animations.unshift(anim)
    else
        for index in [[email protected]] by 1
            if anim.time > @animations[index-1].time and 
            anim.time < @animations[index]
                @animations.splice(index, 0, anim)
                break

The key is that CoffeeScript will not interpret the next line as the body of the statement if the line ends with an operator, so if you want your if statement to span multiple lines then you must end each line with an operator:

if a and 
b and
not c
  do something

Whereas this will not compile:

if a
and b 
not c
  do nothing
Sign up to request clarification or add additional context in comments.

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.