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.

6
  • 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? Commented Mar 28, 2020 at 19:46
  • You might wanna check this function :w3schools.com/jsref/jsref_sort.asp Commented Mar 28, 2020 at 19:49
  • in arr.sort no need to give comparator function Commented Mar 28, 2020 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. Commented Mar 28, 2020 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 stackoverflow.com/questions/6612385/…. Commented Mar 28, 2020 at 20:23

1 Answer 1

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 = []

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.