I'm building small web page to solve the famous Hanoi towers problem. All is fine, however when trying to store state of three towers at each step (255 step for 8 disks) I tried to use array of array or array of objects that contain 3 arrays representing disks in every tower, but only states where there is 8 disks (array items) are stored otherwise there is only empty array! following my code:
class steps //object that store state of towers at each step
{
constructor(src, spr, tgt)
{
this.src = src;
this.spr = spr;
this.tgt = tgt;
}
}
function Hanoi(n, source, target, spare)
{
if(n>0) {
Hanoi(n-1, source, spare, target);
target.push(source.pop());
document.getElementById('output').innerHTML+= source+ " ;" + spare + " ;" + target + "<br/>"; //print data to see what is happening
var obj = new steps(source, spare, target);
console.log(source, spare, target);
src_steps.push(obj); //src_steps is global variable
Hanoi(n - 1, spare, target, source);
}
}
src = [8, 7, 6, 5, 4, 3, 2, 1];
spr = [];
tgt = [];
Hanoi(8, src, spr, tgt);
The problem is that printed array contain the data as it should be, however the object array only contain arrays that contain 8 items or empty but no arrays with less than 8 items! What is happening here?