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));
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:
What you actually want is a destructuring assignment. (thanks to @Reyno for the link!)