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

Tell us what’s happening:

I dont know what’s wrong with my nested for loop. The system won’t let me pass.

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

  if (array[j] > array[j + 1]) { 
    
    
    
    let temp = array[j];
    array[j] = array[j + 1];
    array[j + 1] = temp;

    
  }
}

}

};

Your code so far

<!-- file: index.html -->

/* file: styles.css */

/* file: script.js */
// User Editable Region

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

      if (array[j] > array[j + 1]) { 
        
        
        
        let temp = array[j];
        array[j] = array[j + 1];
        array[j + 1] = temp;

        
      }
    }
  }
 
};


// 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/124.0.0.0 Safari/537.36

Challenge Information:

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

You need to:

  • reset,
  • leave the outer for loop untouched.
  • and try again on your inner loop without the swap code first.
3 Likes

Thanks so much! @WongYC-66