Search code examples
javascriptcoffeescriptdashing

Coffescript switch when


My coffeescript is as following:

level = switch
      when 0 <= value <= 1 then 0
      when 1 < value <= 2 then 1
      when 2 < value <= 3 then 2
      when 3 < value <= 4 then 3
      when 4 < value <= 5 then 4
      else 6

Why do I get :

Uncaught Error: ExecJS::ProgramError: [stdin]:15:4: error: unexpected when when 1 < value <= 2 then 1

This works fine:

 when value <= cool then 0
      when value >= warm then 4
      else 
        bucketSize = (warm - cool) / 3 # Total # of colours in middle
        Math.ceil (value - cool) / bucketSize

This also works:

 level = switch
      when value <= 1 then 0
      when value <= 4 then 4
      else 5

untill I add when value <=2 then 1


Solution

  • It was all about indentation.

    level = switch
          when 0 <= value < 1 then 0
          when 1 <= value < 2 then 1
          when 2 <= value < 3 then 2
          when 3 <= value < 4 then 3
          when 4 <= value <= 5 then 4
          else 5
    

    I kept the code here : http://www.coffeelint.org/

    and then checked the indentation.