0

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.

M A Salman
  • 3,666
  • 2
  • 12
  • 26
  • 2
    Describe what you think your code does (in your post, not as a comment), and then look at your code again: is that really what it does? – Mike 'Pomax' Kamermans Mar 28 '20 at 19:46
  • You might wanna check this function :https://www.w3schools.com/jsref/jsref_sort.asp – codeanjero Mar 28 '20 at 19:49
  • in arr.sort no need to give comparator function – M A Salman Mar 28 '20 at 19:50
  • My only problem is while returning the final array, it just appends the last item of the original array mutliple times. Everything seems to be fine till then but the return is not giving the desired values. If I do finalarr = finalarr.concat(arrnew) , I get all the elements in order but as individual elements in final array. I need to have array of 1, array of 2 etc inside the final array. – user2893258 Mar 28 '20 at 19:57
  • The problem isn't with `finalarr`, but with `newarr`. You are putting the same mutable array in `finalarr` several times. You need to create a new array each time through your loop. See also https://stackoverflow.com/questions/6612385/why-does-changing-an-array-in-javascript-affect-copies-of-the-array. – Luke Woodward Mar 28 '20 at 20:23
  • @LukeWoodward , thanks a ton... have my answer finally :) , worked when I put the newarr inside the loop. Makes sense now. – user2893258 Mar 28 '20 at 20:47

1 Answers1

1

Objects and arrays are pushed as a pointer to the original object . Built-in primitive types like numbers or booleans are pushed as a copy.

In your code your are pushing arrnew in finalarr

finalarr.push(arrnew);

Which means reference of arrnew is pushed in finalarr.

So after first iteration of for loop:

finalarr = [[1,1,1,1]]
arrnew = [1,1,1,1]

Here element of array finalarr is acutally pointer to array arrnew.

So, In the second iteration the statement

arrnew.length = 0

Will empty the array arrnew, as array finalarr have pointer to array it also gets modified:

finalarr= [[]]

Then inner for loop will push three 2 in array arrnew = [2,2,2], so finalarr become [[2,2,2]].

At the end of second iteration finalarr.push(arrnew); will push one new reference of arrnew in finalarr. So finalarr will become

finalarr = [[2,2,2], [2,2,2]]

So at the end of all iterations, finalarr will have 9 pointers, all pointing to arrnew and the final value in arrnew is [591].

finalarr = [[591],[591],[591],[591],[591],[591],[591],[591],[591]]

You can update you code, just replace arrnew.length = 0 with arrnew = []