Variables - jackbackrack/cons-beginners-course GitHub Wiki

hold values and can be reassigned.

  • definition statement -- var s
  • reference expression -- s
  • assignment statement -- s = 10
  • define + assign -- var s = 10
var s = cube({ center: true });
return s.translate([1, 0, 0]);

Multiple Uses of Variables

Variables can be referenced multiple times. Each time a geometry operation (like a transformation or a set operation) is applied to a shape a new shape is produced with that transformation applied. Therefore, we can make one prototype object and use it multiple times with different positions.

var s = cube({ center: true });
var s1 = s.translate([-2, 0, 0]);
var s2 = s.translate([-2, 0, 0]);
return union(s1, s2);

Const versus Var

It is recommended to use const for defining read-only variables, where read-only means they are only defined and not assigned to later.

const s = cube({ center: true });
return s.translate([1, 0, 0]);

Homework

  • use a variable as a prototype
  • use a variable named x to keep track of current x position
    • start with x at zero
    • use assignment to x to increment its value
    • produce three copies of a cube at successive x positions
  • create a box of dimensions 6x6x6 with thickness 1 using a single 6x6x1 board
    • use rotation and translation of that single board