cusX
cusX

Reputation: 500

Variable not holding the right array while pushing into an Array

How can I properly scope my startA variable so that it is the first array that I pushed towards my result array ?

As you can see from my output results, the first array that I am pushing into my results array is actually my end array.

Is this a variable scoping problem ? because, I have pre-define a startA variable that holds the initial start value ([1,1]), why would the output of my startA variable still be [7,7] ? Isn't this a by-value copy since its a primitive type and not a reference type?

// Tick Toward
function tickToward(start, end) {
    var startA = start;
    var results = [];
    results.push(startA);
    for (var i = 0; i < start.length; i++) {
        if (start[i] < end[i] && start[i + 1] < end[i + 1]) {
            start[i] += 1;
            start[i + 1] += 1;
            var a = [start[i],start[i+1]];
            results.push(a);
            // console.log("Condition 1");
            i--;
        } else if (start[i] > end[i] && start[i + 1] > end[i + 1]) {
            start[i] -= 1;
            start[i + 1] -= 1;
            var b = [start[i],start[i+1]];
            results.push(b);
            // console.log("Condition 2");
            i--;
        } else if (start[i] > end[i] && start[i + 1] < end[i + 1]) {
            start[i] -= 1;
            start[i + 1] += 1;
            var c = [start[i],start[i+1]];
            results.push(c);
            // console.log("Condition 3");
            i--;
        } else if (start[i] < end[i] && start[i + 1] > end[i + 1]) {
            start[i] += 1;
            start[i + 1] -= 1;
            var d = [start[i],start[i+1]];
            results.push(d);
            // console.log("Condition 4");
            i--;
        } else if (start[i] > end[i] && start[i + 1] == end[i + 1]) {
            start[i] -= 1;
            var e = [start[i],start[i+1]];
            results.push(e);
            // console.log("Condition 5");
            i--;
        } else if (start[i] < end[i] && start[i + 1] == end[i + 1]) {
            start[i] += 1;
            var f = [start[i],start[i+1]];
            results.push(f);
            // console.log("Condition 6");
            i--;
        } else if (start[i] == end[i] && start[i + 1] > end[i + 1]) {
            start[i + 1] -= 1;
            var g = [start[i],start[i+1]];
            results.push(g);
            // console.log("Condition 7");
            i--;
        } else if (start[i] == end[i] && start[i + 1] < end[i + 1]) {
            start[i + 1] += 1;
            var h = [start[i],start[i+1]];
            results.push(h);
            // console.log("Condition 8");
            i--;
        }
    } console.log(results);

}


tickToward([1, 1], [7, 7]); // Output should be [ [ 1, 1 ], [ 2, 2 ], [ 3, 3 ], [ 4, 4 ], [ 5, 5 ], [ 6, 6 ], [ 7, 7 ] ]

// However, my output is [ [ 7, 7 ], [ 2, 2 ], [ 3, 3 ], [ 4, 4 ], [ 5, 5 ], [ 6, 6 ], [ 7, 7 ] ]

Here is the question if anyone is interested: https://i.sstatic.net/5UR5T.png

Upvotes: 1

Views: 45

Answers (1)

Zee
Zee

Reputation: 8488

Isn't this a by-value copy since its a primitive type and not a reference type?

var startA = start;

This line is causing the problem. It creates a reference and not a individual copy.

You need to use 'slice()' which will clone the array.

var startA = start.slice();

Demo

Upvotes: 2

Related Questions