I’ll be honest I’m not too sure what else its asking of me. Before decrementing J, assign the value at J to the index J + 1. Seems pretty cut and dry but it doesn’t pass anyway I try it.
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) {
let 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
You are assigning j+1 to j (which this variable has declared before and no need to update that way because it is updating with reduced). However, you should modify something in the array; the main purpose of these for and while loops is to sort the array variable. But nothing change of the array.
You are accessing the value of each array element by the i index inside the for loop and assigning it to the currValue variable. After that, you declare the j variable with i-1, which means j will point to the previous index, and the while loop will never work with a value less than zero.
Additionally, there is one more condition for the while loop: array[j] should be greater than currValue, which means array[i-1] > array[i]. If this condition is true, you should proceed with sorting the array elements. For this, you should shift the biggest element to the next index.
So lesson says;
Do so by assigning the value at the j index to the next index.
Take to j index element value and assign to next index until this while loop end. And the biggest number will be stored next of the j index and you will continue to update of the for loop’s element for prevent to dublication and loop will continue to until array length and while loop sort to bigest numbers to next and smallest stay on the before them. If you want to understand better, just continue to lesson and it will show you well.
I don’t think the requirements are super clear. Jumping into this step without having read the code or fully understanding what is supposed to happen, I read it like you did at first as well, but without the redeclaration.
However, “assign to” and “value at index” refers to array positions.
You should assign the element at array index j to the array at index j + 1. That is, you are reading from and writing to the array at the index positions given.
it is definitely not clear. However, I do not think I was taught how to assign and element at a certain array index to equal another array index yet, so none of this is making sense to me lol.
Do you remember how to access an element of an array, or how to change the value of an element in an array? But first of all, do you remember the aim of this lesson? What is it trying to show you?
I tried to remind you of the aim of this lesson in my first comment, and @lasjorg also explained it well what you should do this step.
But if you don’t remember any of this, it’s okay. Just try to restart this lesson if you want to learn it clearly, and review what you learned about arrays before. Alternatively, you can finish the ‘Review JavaScript Fundamentals by Building a Gradebook App’ (assuming you haven’t completed it yet, as it was added recently). Please try to focus on one lesson at a time and avoid taking long breaks, as you may forget the context.
Happy coding.
What they’re trying to say in very convoluted confusing terms, is that you are assigning [J + 1] to J. What you want to do is assign j to [j + 1] Also use the array [index].This was WAY more confusing than it needed to be.