3 ms·
> One part that confused me is how I needed to use let() to define startColumn and startRow inside the loop. I don’t understand this… That's because you wrote
by nayuki 10mo ago
> One part that confused me is how I needed to use let() to define startColumn and startRow inside the loop. I don’t understand this…
That's because you wrote a for-loop without braces, so it can only contain a single statement, and that single statement is your let().
Here is an equivalent way to write that section of code (and I did test it):
for (r = [ 1 : numRows ]) {
startColumn = ((c * thicknessWall) + ((c - 1) * batteryType));
startRow = ((r * thicknessWall) + ((r - 1) * batteryType));
translate([startColumn, startRow, thicknessWall])
cube([batteryType, batteryType, heightCompartment + 1]);
}
Side note, related to declaring variables but not an issue that you encountered: OpenSCAD does not allow setting the value of a variable more than once. So this code is illegal:
x = 1;
echo(x);
x = 2;
echo(x);
Second note is that whenever you introduce a construct such as {union(), difference(), if(), for()}, variables declared within the braces are scoped, so they don't exist between iterations or after the scope ends.
if (true) { y = -1; }
echo(y); // Illegal
However, just introducing braces without a construct does not restrict the variable to the scope:
{ z = 5; }
echo(z); // Legal