# Bug in Insertion Sort Code

**URL:** https://forum.kirupa.com/t/bug-in-insertion-sort-code/663699
**Category:** programming
**Created:** [December 15, 2023, 8:07pm UTC](https://forum.kirupa.com/t/bug-in-insertion-sort-code/663699 "2023-12-15T20:07:30Z")
**Posts on this page:** 1
**Showing post:** 2

<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, 3:17am UTC](https://forum.kirupa.com/t/bug-in-insertion-sort-code/663699/2 "2023-12-16T03:17:09Z")

</div>

Thanks! I’ve gone ahead and updated the errata thread with this! 🙂

> [@Errata: Data Structures and Algorithms Book!](http://forum.kirupa.com/t/errata-data-structures-and-algorithms-book/663668/5):
>
> Insertion Sort - Fixed undeclared j variable The following code declares the j variable correctly to avoid any out-of-scope errors: function insertionSort(input) { // Variable to store the current element being compared let activeNumber; // Loop through the array starting from the second element (index 1) for (let i = 1; i \< input.length; i++) { // Store the current element in the activeNumber variable activeNumber = input[i]; let j; // Inner loop to compare the …

---

_[View the full topic](https://forum.kirupa.com/t/bug-in-insertion-sort-code/663699)._
