# found a bug in Absolute Beginner’s Guide to Algorithms

**URL:** <https://forum.kirupa.com/t/found-a-bug-in-absolute-beginner-s-guide-to-algorithms/663637>\
**Category:** programming\
**Created:** [December 13, 2023, 8:30pm UTC](https://forum.kirupa.com/t/found-a-bug-in-absolute-beginner-s-guide-to-algorithms/663637 "2023-12-13T20:30:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![transbot](https://avatars.discourse-cdn.com/v4/letter/t/4bbf92/32.png) [@transbot](https://forum.kirupa.com/u/transbot)\
**Post date:** [December 13, 2023, 8:30pm UTC](https://forum.kirupa.com/t/found-a-bug-in-absolute-beginner-s-guide-to-algorithms/663637/1 "2023-12-13T20:30:30Z")

</div>

I found a bug in your code in your book. The following code:

```auto
// Recursive Approach
function binarySearch(arr, val, start = 0, end = arr. {
  const middleIndex = Math.floor((start + end) / 2);
  if (val === arr[middleIndex]) {
    return middleIndex;
  }
  if (start >= end) {
    return -1;
  }
  if (val < arr[middleIndex]) {
    binarySearch(arr, val, start, middleIndex - 1);
  } else {
    binarySearch(arr, val, middleIndex + 1, end);
  }
}

```

Should be corrected to:

```auto
// Recursive Approach
function binarySearch(arr, val, start = 0, end = arr. {
  const middleIndex = Math.floor((start + end) / 2);
  if (val === arr[middleIndex]) {
    return middleIndex;
  }
  if (start >= end) {
    return -1;
  }
  if (val < arr[middleIndex]) {
    return binarySearch(arr, val, start, middleIndex - 1);
  } else {
    return binarySearch(arr, val, middleIndex + 1, end);
  }
}

```

Make sure to include the `return` statement in the recursive calls to `binarySearch` to fix the issue.

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [December 15, 2023, 3:57am UTC](https://forum.kirupa.com/t/found-a-bug-in-absolute-beginner-s-guide-to-algorithms/663637/2 "2023-12-15T03:57:17Z")

</div>

Wow! You are absolutely right. Thanks for flagging this. I’ve documented it in the [errata thread](http://forum.kirupa.com/t/errata-data-structures-and-algorithms-book/663668) and have also flagged it for correction in future printings.
