Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

65
Views
Correct bubble sort in JS doesn't do anything

The code just logs the same array. What seems to be wrong here? I'm using VS Code.

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

let arr1 = [9, 4, 6, 4, 3];

console.log(bubbleSort(arr1));

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It should be:

[array[j], array[j + 1]] = [array[j + 1], array[j]];

instead of:

[array[j], array[j + 1] = array[j + 1], array[j]];

Pay close attention to where the square brackets begin and end.


Your current code:

  1. assigns one array index to itself
  2. creates an array with 3 elements
  3. and throws it away immediately

What you actually want is a destructuring assignment. (thanks to @Reyno for the link!)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!