新手问题 CoffeeScript error: unexpected POST_IF

douya0808 · 2013年05月03日 · 最后由 douya0808 回复于 2013年05月03日 · 4136 次阅读
odd=(num)->
    if typeof num is 'number'
    if num is Math.round num
        if num > 0
           num % 2 is 1
        else
           throw "#{num} is not positive"
        else 
       throw "#{num} is not an integer"
    else 
       throw "#{num} is not a number"   

总是报这个错误

C:\>coffee odd.coffee
odd.coffee:2:5: error: unexpected POST_IF
    if typeof num is 'number'
    ^^

来给我些关怀吧

else
   throw "#{num} is not positive"
else  #应该是end

大哥你好 可能是我贴代码的时候排版没弄好 那个 else 是第二个 if 的

odd=(num)->
    if typeof num is 'number'
        if num is Math.round num
             if num > 0
                num % 2 is 1
             else
                throw "#{num} is not positive"
        else 
           throw "#{num} is not an integer"
    else 
       throw "#{num} is not a number"
odd=(num)->
   if typeof num is 'number'
      if num is Math.round num
         if num > 0
            num % 2 is 1
         else
            throw "#{num} is not positive"
      else 
          throw "#{num} is not an integer"
   else 
       throw "#{num} is not a number"

哦,应该是你没有排好

#4 楼 @kaka 大哥 我感觉我和你写的一模一样啊 为啥你的就好使呢?

@douya0808 缩进不对,注意缩进

我是放到 coffeescript 官网编译... 可以通过

撸主的缩进错了吧

源码如果缩进没错的话 没问题

@zj0713001 屏幕截的大啊 😏 哈哈

多谢楼上各位大哥 刚接触 coffee 鼻青脸肿的

我什么都没动 刚才还好用 然后再试 又报下面的错误

ReferenceError: odd is not defined
    at repl:1:5
    at REPLServer.replDefaults.eval (C:\Documents and Settings\Administrator\App
lication Data\npm\node_modules\coffee-script\lib\coffee-script\repl.js:28:28)
    at repl.js:239:12
    at Interface.<anonymous> (C:\Documents and Settings\Administrator\Applicatio
n Data\npm\node_modules\coffee-script\lib\coffee-script\repl.js:58:9)
    at Interface.EventEmitter.emit (events.js:95:17)
    at Interface._onLine (readline.js:202:10)
    at Interface._line (readline.js:531:8)
    at Interface._ttyWrite (readline.js:754:14)
    at ReadStream.onkeypress (readline.js:99:10)
    at ReadStream.EventEmitter.emit (events.js:117:20)

哎我去了 弄死我吧

话说 coffeescript 还严格要求缩进吗?

我想我知道错在呢了

C:\>coffee odd.coffee

C:\>coffee
coffee>coffee odd.coffee
ReferenceError: odd is not defined
    at repl:1:5
    at REPLServer.replDefaults.eval (C:\Documents and Settings\Administrator\App
lication Data\npm\node_modules\coffee-script\lib\coffee-script\repl.js:28:28)
    at repl.js:239:12
    at Interface.<anonymous> (C:\Documents and Settings\Administrator\Applicatio
n Data\npm\node_modules\coffee-script\lib\coffee-script\repl.js:58:9)
    at Interface.EventEmitter.emit (events.js:95:17)
    at Interface._onLine (readline.js:202:10)
    at Interface._line (readline.js:531:8)
    at Interface._ttyWrite (readline.js:754:14)
    at ReadStream.onkeypress (readline.js:99:10)
    at ReadStream.EventEmitter.emit (events.js:117:20)
coffee>

为什么在文件所在路径 用 coffee odd.coffee 就可用 换成 coffee>执行指令就不可行了呢

难道 coffee>仅仅是用来在命令行中执行 coffeescript 的吗

需要 登录 后方可回复, 如果你还没有账号请 注册新账号