0

The following code:

binary_search = (arr, val)->
    arr = arr.concat(arr)
    lower = 0 ; upper = arr.length-1
    while upper-lower > 1
        mid = Math.floor((lower+upper)/2)
        if arr[mid] is val
            return mid
        if arr[mid] < val
            if arr[mid-1] < arr[mid+1]
                low = mid+1
            else 
                up = mid-1
        else
            if arr[[mid-1] > arr[mid+1]
                low = mid+1
            else 
                up = mid-1
    return -1

and get the following error:

   error: unmatched OUTDENT
            up = mid-1

What am I doing wrong?

tldr
  • 11,924
  • 15
  • 75
  • 120
  • 2
    Syntax error here: `arr[[mid-1]`. Apart from that, you are creating an empty function because the whole code block is not indented properly. – Felix Kling Dec 03 '13 at 05:20
  • Btw, I actually don't know CoffeeScript, but here is how I figured out the problem (maybe you can learn from it): I copied your code to http://coffeescript.org/, "Try CoffeeScript". It said there is an "unmatched OUTDENT" error in line 17. Googling for "unmatched OUTDENT" returned [this SO question](http://stackoverflow.com/q/14269529/218196) as first hit(!). It mentions unmatched parenthesis. If you then look for unmatched parenthesis before line 17, you can easily find `arr[[mid-1]`. Once the syntax error is fixed, the code compiles and you can see that `binary_search` is empty. – Felix Kling Dec 03 '13 at 05:33
  • thanks! It was the extra bracket there. If you add your comment as an answer, I'll accept it. – tldr Dec 03 '13 at 06:08

0 Answers0