Ned
Ned

Reputation: 371

pushing new array into a 2d array


I'm trying to push a new array into a global array here:

var history = [ ];

function addHistory(array)
{
history.push(array);
console.log(history)//to check what is the value of array
}

var test1 = [0,0,0,0,0];
var test2 = [1,1,1,1,1];
addHistory(test1);
addHistory(test2);

After doing like this, the array should be:

[ [0,0,0,0,0] , [1,1,1,1,1] ]

But instead it prints

[ [1,1,1,1,1] , [1,1,1,1,1] ]

So basically it replaces all old arrays in array "history", instead of pushing it at the end.

What can be wrong here?
Thanks a lot

EDIT:
Sorry, forgot to mention that my actual variable is not called history (I called it that way just that you can imagine what I want). It is called "rollHist"

Upvotes: 2

Views: 67

Answers (1)

millerbr
millerbr

Reputation: 2961

history is a protected term in javascript. Changing it to this will fix it:

var myHistory = [];

function addHistory(array)
{
myHistory.push(array);
console.log(myHistory)//to check what is the value of array
}

var test1 = [0,0,0,0,0];
var test2 = [1,1,1,1,1];
addHistory(test1);
addHistory(test2);

You can read more about the various protected words here

Upvotes: 2

Related Questions