2


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"

Ned
  • 361
  • 3
  • 16
  • Sorry for my mistake, it's the same with every name.. Maybe there is another way of inserting new array into an array? – Ned Feb 28 '16 at 17:03

1 Answers1

2

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

millerbr
  • 2,951
  • 1
  • 14
  • 25