Learn Basic Algorithmic Thinking by Building a Number Sorter - Step 38

Tell us what’s happening:

Help! I don’t know what’s wrong, been looking at several other posts and still can’t get it, thanks!

Your code so far

<!-- file: index.html -->

/* file: styles.css */

/* file: script.js */
// User Editable Region

const insertionSort = (array) => {
  for (let i = 1; i < array.length; i++) {
    const currValue = array[i];
    let j = i - 1;

    while (j >= 0 && array[j] > currValue) {
      j = j+1;
       array[j] = array[j + 1];
      j--;
    }
  }
}

// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36

Challenge Information:

Learn Basic Algorithmic Thinking by Building a Number Sorter - Step 38

by assigning the value at the j index to the next index.

You are assigning the value at j+1 to the index at j

If I assign 17 to ‘a’:

a = 17
1 Like

I still get an error…
const insertionSort = (array) => {
for (let i = 1; i < array.length; i++) {
const currValue = array[i];
let j = i - 1;

while (j >= 0 && array[j] > currValue) {
  j = j+1;
  array[j + 1] = array[j];
  j--;
}

}
}

Hi there!

Remove that extra assignment line.

1 Like

Thanks Hasan but please explain why this helped (it worked!)

1 Like

Because the challenge step asking in the challenge instructions to add that second line of assignment.

j = j+1; //add one to j
  
  j--; //remove one from j

Your loop will run forever adding one and then subtracting 1 from j

1 Like