# Learn Basic Algorithmic Thinking by Building a Number Sorter - Step 27

### Tell us what’s happening:

Hello everybody! I just done what is said - nothing helps. It doesn’t work. Can’t pass to the next step!

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.

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

Hello suharnikov

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.

I hope this helps

1 Like

1 Like

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

is it not the way it shoud be?

This is statement determines if a switch needs to be made. You should swap the values here, not outside of the for loop.

1 Like

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

The matter is that you didn’t do it where the tests expected you to do so, so the tests are confused by your code

1 Like

It litteraly says: “After your nested for loop, you should declare a temp variable.” after i tried all this swapping - the code is:

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

}
}

Ah, mixed up my sorts, it does say ‘after the loop’.

Try swapping these two lines. Think about the order for a sec - this order doesn’t do what you want.

1 Like

it is this again

with this code:

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[i]
array[minIndex] = array[i];
array[i] = temp;
}
}

}
}

Put this back after the loop

Also, don’t change the first line.

1 Like

read carefully the error message, you are doing the opposite of what’s written there

1 Like

iamsonofagun! THANK YOU!