I see this question has come up a few times on here, but mine is a bit different. I'm new to JavaScript this is a basic program to show the problem.
var iterations = 0;
function someFunc(x, y, z) {
for (var i=0; i<4; i++) {
x[i] = x[i] * 2;
y[i] = y[i] * 2;
z[i] = z[i] * 2;
}
iterations++;
if (iterations >= 10)
return {done:true, x, y, z};
else
return {done:false, x, y, z};
}
function main() {
var x = [0, 0, 0, 0];
var y = [1, 1, 1, 1];
var z = [2, 2, 2, 2];
done = false;
while (!done) {
let {done, x, y, z} = someFunc(x, y, z);
console.log(x, y, z);
// Do some other stuff with x,y,z here,
// like calling anotherFunc(x, y, z)
}
}
main();
I get an error on the line with the call to someFunc. The error is "Exception Occurred: Reference error: x is not defined".
So what I'm doing is calling a function to update some arrays each time around a loop. I need to be able to get those arrays back out from the function called 'someFunc' so that I can pass them to another function to do some other work on them.
Then I need to feed them back into the first function again... and so on around and around the loop until I have finished.
I'm coming from Python where calls like
a, b, c = someFunc(a, b, c)
are fine.
But I have no idea how to proceed with JavaScript. Any help would be much appreciated. Happy to clarify if my question is not totally clear.