Here is a code snippet that aims to convert a string into an array of numbers and then sort them in descending order. The objective is to find an alternative to the sort() method.
However, there seems to be an issue with the current implementation. When the number 7 is placed in the first half of the array (as shown in the example), the code malfunctions. Interestingly, if 7 is swapped with a number larger than the last one (as done with 22 in the example), the code works correctly.
The goal is to ensure that the code works properly regardless of the positioning of the numbers.
var row = '92 43 7 119 51 22';
var row = row.split(' ');
var column = row.map(Number);
function arrangeNum(column) {
for (var i = 0; i <= column.length - 1; i++) {
for (var j = column.length - i; j >= 0; j--) {
if (column[j] > column[j - 1]) {
var temp = column[j];
column[j] = column[j - 1];
column[j - 1] = temp;
}
}
}
return column;
}
console.log(arrangeNum(column));