Search up definitions

59 Views Asked by At

This is my code for livescript:

                        if (res = json.msg.match /^iSearchup\s(.*)$/i)? then
                            getReq("[url='http://urbanscraper.herokuapp.com/define/'][http://urbanscraper.herokuapp.com/define/[/url]" + encodeURIComponent(msg.splice(1, msg.length - 1).join(" ")) + ".json", function(res, passback)
                            if (res.word && res.definition){
                                @socket.send JSON.stringify {
                                    type: \pmsg
                                    nick: 'iPoddy:'
                                    msg: json.from + ": " + res.word + " - " + res.definition
                                }
                            }
                            else {
                                @socket.send JSON.stringify {
                                    type: \pmsg
                                    nick: 'iPoddy:'
                                    msg: json.from + ":  Sorry, no results were returned."
                                }
                            }

That is my code. It gave me the error "dedent" but I fixed it and it still give me that error again. Help?

1

There are 1 best solutions below

0
On

You have several errors in this code. It will give you an error of UNEXPECTED DEDENT because you are mixing spaces and tabs and LiveScript is a whitespace-strict programming language. So on, avoid the use of brackets while using LiveScript. Remember also that in a conditional structure, like if, you might use then after. This will not compile due to:

  • Mixing spaces and tabs
  • else keyword with brackets
  • Expecting then after expression to determine condition
  • Syntax Error in function(res, passback)

And you have some little issues and not standardized practices:

  • Using assignment operator = instead of ==|~= for comparasion
  • Using an invalid lambda expression as a parameters in function(res, passback)
  • Using + to concat, instead of ++
  • Using && operator, instead of and
  • Using parentheses to encapsulate a single expression in the first conditional

This should work well:

if res ~= json.msg.match /^iSearchup\s(.*)$/i then
    url = "[url='http://urbanscraper.herokuapp.com/define/'][http://urbanscraper.herokuapp.com/define/[/url]"
    getReq <| url ++ encodeURIComponent(msg.splice(1 msg.length - 1).join(" ")) ++ ".json"
    if res.word && res.definition then
        @socket.send <| JSON.stringify(
            type: \pmsg
            nick: 'iPoddy:'
            msg: json.from ++ ": " ++ res.word ++ " - " ++ res.definition
        )
    else
        @socket.send <| JSON.stringify (
            type: \pmsg
            nick: 'iPoddy:'
        msg: json.from ++ ":  Sorry, no results were returned."
        )