# Errata: Data Structures and Algorithms Book!

**URL:** https://forum.kirupa.com/t/errata-data-structures-and-algorithms-book/663668
**Category:** programming
**Created:** [December 15, 2023, 3:54am UTC](https://forum.kirupa.com/t/errata-data-structures-and-algorithms-book/663668 "2023-12-15T03:54:20Z")
**Posts on this page:** 1
**Showing post:** 3

<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 16, 2023, 2:55am UTC](https://forum.kirupa.com/t/errata-data-structures-and-algorithms-book/663668/3 "2023-12-16T02:55:38Z")

</div>

**Iterative Binary Search has an undefined right variable**  
The correct version is:

```js
// Iterative Approach
function binarySearch(arr, val) {
  let start = 0;
  let end = arr.length - 1;

  while (start <= end) {
    let middleIndex = Math.floor((start + end) / 2);

    if (arr[middleIndex] === val) {
      return middleIndex;
    } else if (arr[middleIndex] < val) {
      start = middleIndex + 1;
    } else {
      end = middleIndex - 1;
    }
  }

  return -1;
}

```

The incorrect terminating condition for the `while` loop had `start <= right`, where `right` wasn’t defined.

---

_[View the full topic](https://forum.kirupa.com/t/errata-data-structures-and-algorithms-book/663668)._
