0

I have a function that tries to systematically add arrays to another multidimensional array. At each step of the way the arrays being added are calculated correctly, however, these calculations change the previously entered values. I've tried using slice but I'm clearly doing it wrong :(.

Please see code below - it is the return posMatrix that is being affected.

function allPossibilities(hand) {

    var startingHandLength = hand.length;
    var potHand = Array.prototype.slice.call(hand);
    var scores = new Array();
    var posMatrix = new Array();
    var nextCard = 1;
    var progressStage = true;
    var finished = false; 
    var shallowArr = new Array();  

    do {
         scores = calculateScores(potHand);
    var maxScore = Math.max.apply(null, scores)


    shallowArr = potHand.slice();

    if (maxScore>16.5)
        {posMatrix.push([shallowArr,maxScore])
         console.log(posMatrix);
         debugger;

            if (potHand.length !== startingHandLength)
                {
                    do{

                        if(potHand[potHand.length-1][1] < 10)
                             {
                                    potHand[potHand.length-1][1]++;
                                    progressStage = true;

                            }
                        else {potHand.pop();

                        potHand[potHand.length-1][1]++;}

                        } 
                        while(progressStage === false)

                }

        }
    else
        {

        potHand.push(["Imaginary",1,"Imaginary"]);

        }

    progressStage=false;

    if(potHand.length === startingHandLength)
        {finished = true;}


    }
    while(finished === false);


        return posMatrix;


}

If the starting hand > 16.5, the function works as none of the other code gets to run. But otherwise it does not. The final return should be an array where each element is looks like this: [[array],number]. The number seems to come out fine, but since it is not an object it is not affected. I would expect the [array]s to be different from one another, currently they are all the same.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
HarryShotta
  • 347
  • 1
  • 15
  • 1
    Possible duplicate of [Fastest way to duplicate an array in JavaScript - slice vs. 'for' loop](https://stackoverflow.com/questions/3978492/fastest-way-to-duplicate-an-array-in-javascript-slice-vs-for-loop) – Eddie Apr 04 '19 at 17:26

2 Answers2

1

Slice returns a shallow copy of array, since you have multidimensional array so you need to deep clone of array

JSON.parse(JSON.stringify(array))

Or you can use loadash cloneDeep

Code Maniac
  • 37,143
  • 5
  • 39
  • 60
0

You made a shallow copy of hand (which, BTW, you should've included). With statements like this

potHand[potHand.length-1][1]++;

you're accessing and modifying elements of hand, too.

Here, potHand[potHand.length-1] is an object, and it's en element of hand (not a copy - the same element).

mbojko
  • 13,503
  • 1
  • 16
  • 26