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

On each iteration of your while loop, it is finding an element that is larger than your current value. You need to move that element to the right to make room for your current value.

Do so by assigning the value at the j index to the next index:-

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--;      
}

}
}

WARNING

The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.

You will need to take an additional step here so the code you wrote presents in an easy to read format.

Please copy/paste all the editor code showing in the challenge from where you just linked.

Replace these two sentences with your copied code.
Please leave the ``` line above and the ``` line below,
because they allow your code to properly format in the post.

Your browser information:

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

Challenge Information:

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

Hi there, your so close!!, to move the element to the right you want add + 1 to the array first, so Mod edit: removed Well done

1 Like

Please dont give code to pass the challenge

Cody_Biggs, Thanks your help, I’m a little confused here, do you mean I should increase the array length +1 or how to add +1 to the array?

Cody_Biggs, I got it and solved, thks a lot

1 Like

Sorry, I am also stuck at this step. can anyone explain more?

Yeah, I’ve just lost 1h bashing my head around it until I finally noticed the wording, just think about it:
" Do so by assigning the value at the j index to the next index."
If I am adding j to something, how would I write it in code? :slight_smile:

I still don’t understand what the solution is. Could you elaborate more?

Let’s quickly recap what an array is.

you’ve got “someName[j]” where “someName” is the name of the array and “j” is the index.
Now how do arrays work? They start at number 0 and go up to the maximum value, which you usually define within your “for” statement.
So if you had someName[3] it would go like someName[0], someName[1], someName[2]…you get the point now?

So how do you assign something to something else?
if I had a=0, b=1, c=2 and pretended that abc are indexes, how would I assign b to c?

Notice that writing for example a=0 means I am assigning 0 to a. That’s the nearest tip I can give you without revealing the exact code solution…

I’m stuck in this step and I still don’t understand… uff!

I have the same question , what is the answer?
none of the following have worked :
array[j] = array[j] + 1;
array[j] = array[j + 1];
j = array[j] + 1;
j = array[j + 1];
j = j + 1;

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Ask for Help button located on the challenge (it looks like a question mark). This button only appears if you have tried to submit an answer at least three times.

The Ask for Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.