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

Tell us what’s happening:

I’m completely stuck. Please help me. Nothing I submit is working.

Your code so far

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) {
  array[j] = j+1;
  j--;
}

}
}

<!-- 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) {
      array[j] = j+1;
      j--;
    }
  }
}

// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36

Challenge Information:

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

Hello;

The step says,

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

You should take the value of the element at the j index from the array and assign it to the next index. However, you are assigning the directly j+1 value to your j indexed element.

Hi @abdullamaseeh123

Before decrementing j, assign the value at j to the index j + 1.

You are assigning the wrong way around.

Also, how is this accessing the index?

Happy coding