Tell us what’s happening:
I am able to solve the challenge by following the instructions but I am having trouble understanding what is going on. I hope you don’t mind helping.
I don’t understand what the j-- is doing.
So here is the logic as i understand it:
currValue = 2;
array[j] = 8;
therefore: array[j] is greater than currValue;
decrement j;
j is already 0, correct? what does j-- do? j is now -1? what does that even mean?
is my logic sound?
help!
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) {
decrement 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/133.0.0.0 Safari/537.36
Challenge Information:
Learn Basic Algorithmic Thinking by Building a Number Sorter - Step 38