Hello everybody! I just done what is said - nothing helps. It doesn’t work. Can’t pass to the next step!
Your code so far
const selectionSort = (array) => {
for (let i = 0; i < array.length; i++) {
let minIndex = i;
for (let j = i + 1; j < array.length; j++) {
console.log(array, array[j], array[minIndex]);
if (array[j] < array[minIndex]) {
minIndex = j;
}
}
const temp = array[minIndex];
array[i] = temp;
array[minIndex] = array[i];
}
}
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/121.0.0.0 Safari/537.36 Edg/121.0.0.0
Challenge Information:
Learn Basic Algorithmic Thinking by Building a Number Sorter - Step 27
I was just stuck on this same issue. So, what you want to do, is create the temp variable, just as a storing mechanism to change the value from array[i] to array[minIndex]. When you are done with the code segment, what you want to end up with is basically:
The value you had at array[i], is now at array[minIndex]
The value you had at array[minIndex] is now at array[i].
You use the temp variable to store the values during that swap.
that is the problem. i do exactly that - look the code i posted. but it doesn’t work for me.
I write:
const temp = array[minIndex];
array[i] = temp;
array[minIndex] = array[i];
it is not the instructions that are confusing to me. But I don’t understand why the code above is wrong and what is wrong with it? Console keeps showing message - You should assign array[i] to temp . - BUT I HAVE dear console what is the matter