Hi,
Welcome to the forum!
Your error comes from the fact that you are assigning stage1
to the variable fase
before the variable stage1
has been initialized.
The computer reads your program from top to bottom but in JavaScript it doesn’t give you an error. You can refer to this previous thread on the subject :
This is what happens :
- The JavaScript engine parse your code. Because of hoisting, it moves the declaration of the
stage1
variable so it’s doing :
var stage1; // Uninitialized -> undefined
- Then we declare and assign
stage1
tofase
which isundefined
:
var fase = stage1; // -> undefined
- The
stage1
variable gets initialized with the array :
var stage1 = [
[‘0’, ‘1’, ‘0’, ‘1’],
[‘0’, ‘1’, ‘1’, ‘0’],
];
But fase
is still undefined!!
Check out this simple example :
var a = b;
console.log(a); // Prints undefined
var b = [0, 1];
console.log(a, b); // Prints undefined [0, 1]
So you need to assign stage1
to fase
after stage1
has been initialized