Problem statement :
Clean the room function:
Input
[1,2,4,591,392,391,2,5,10,2,1,1,1,20,20]
,
make a function that organizes these into individual array that is ordered.
For example answer(ArrayFromAbove) should return:
[[1,1,1,1],[2,2,2], 4,5,10,[20,20], 391, 392,591].
My Code:
const arrNum = [1,2,4,591,392,391,2,5,10,2,1,1,1,20,20] ;
function org(arr) {
let finalarr = [];
let arrnew = [];
let val = 0;
arr.sort((function(a, b){return a-b}));
//console.log(arr);
for (i=0; i<arr.length; i++){
if (arr[i] != 0) {
val = arr[i];
arrnew.length = 0;
arrnew.push(arr[i]);
arr[i] = 0;
for (j=0; j<arr.length; j++){
if (arr[j] == val && arr[j] != 0) {
arrnew.push(arr[j]);
arr[j] = 0;
}
}
finalarr.push(arrnew);
console.log(arrnew);
}
}
return finalarr;
console.log(finalarr)
}
org(arrNum)
But this doesn't seem to give desired answer : Not sure what I am doing wrong. I have found the other solutions but I need to know what is wrong in my code please.