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

Tell us what’s happening:i am trying to declare a tem variable after the for loop which i thought i already did but my code still wont pass

Your code so far

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.

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

    }


  }
}

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

the hint says

After your nested for loop, you should declare a temp variable.

individuate the end of the loop and write after it

like this:

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

    
  let temp = array[i];
            array[i] = array[minIndex];
            array[minIndex] = temp;
    
  }

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

it says “nested for loop”, there are two loops in the code, make sure you write after the correct one

that is what i thought i did here.

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

I can’t follow your parenthesis, from what you posted it looks like there is an extra one, make sure you have exactly one closing for each opening

how about now

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

this loop should have a lot of stuff inside, shouldn’t it?

why don’t you share all the code in the editor? it’s difficult to look for syntax error with only a portion

but remember, you need to write your code inside the outer loop, so I suggest you reset the code and try again

here is all the code:

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


  }```

All of it, you can select it all with Ctrl+A

okay


const sortInputArray = (event) => {
  event.preventDefault();

  const inputValues = [
    ...document.getElementsByClassName("values-dropdown")
  ].map((dropdown) => Number(dropdown.value));

  const sortedValues = selectionSort(inputValues);

  updateUI(sortedValues);
}

const updateUI = (array = []) => {
  array.forEach((num, i) => {
    const outputValueNode = document.getElementById(`output-value-${i}`);
    outputValueNode.innerText = num;
  })
}

const bubbleSort = (array) => {
  for (let i = 0; i < array.length; i++) {
    for (let j = 0; j < array.length - 1; j++) {
      if (array[j] > array[j + 1]) {
        const temp = array[j];
        array[j] = array[j + 1];
        array[j + 1] = temp;
      }
    }
  }

  return array;
}

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


  }
}

sortButton.addEventListener("click", sortInputArray);```

you are missing the code to add this step

this is the code:


Like you did in your bubble sort, use a temp variable to extract the value at i, then swap the values at i and minIndex.                                         
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;
      }
    }


  }
}```

I don’t see any temp variable here

i tried to individuate each loop before adding the temp variable and it says same thing.

here:

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

please format your code approrpiately… you can let the editor do that by right clicking and selecting “Format Document”

Also I still can’t see the whole code, please post the whole code

there was an IF statement inside this loop, you removed it, please do not change any of the code

you mean like this:


const sortInputArray = (event) => {
  event.preventDefault();

  const inputValues = [
    ...document.getElementsByClassName("values-dropdown")
  ].map((dropdown) => Number(dropdown.value));

  const sortedValues = selectionSort(inputValues);

  updateUI(sortedValues);
}

const updateUI = (array = []) => {
  array.forEach((num, i) => {
    const outputValueNode = document.getElementById(`output-value-${i}`);
    outputValueNode.innerText = num;
  })
}

const bubbleSort = (array) => {
  for (let i = 0; i < array.length; i++) {
    for (let j = 0; j < array.length - 1; j++) {
      if (array[j] > array[j + 1]) {
        const temp = array[j];
        array[j] = array[j + 1];
        array[j + 1] = temp;
      }
    }
  }

  return array;
}

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





sortButton.addEventListener("click", sortInputArray);```

please reset the step, you have changed stuff you should not change, the if should be inside the for loop