Today we’re going to learn about arrays, which is the simplest way to group information together. We’re then going to learn how to work our way through each item in the collection so that we can draw drawing some significantly more interesting diagrams.
------------------------------------------------------------------------------------
--- Code snippet
------------------------------------------------------------------------------------
cs()
home()
pu()
const NAME = "James"
const AGE = 10 + 5
const NAMES = [ "Lucy", "Robert", "Nora", "Jimme" ]
const RAINFALLS = [ 10, 15, 12, 0, 5 ]
function write_and_step(value_to_write) {
const FONT_SIZE = 1.5
const STEP_SIZE = 10
write(value_to_write, FONT_SIZE)
fd(STEP_SIZE)
}
write_and_step(NAMES[0])
write_and_step(NAMES[2])
write_and_step(NAMES[20])
write_and_step(NAMES.length)
const COLORS = [ "red", "green", "blue", "yellow", "pink", "white", "brown", "purple" ]
write_and_step(COLORS[0])
write_and_step(COLORS[1])
write_and_step(COLORS[2])
write_and_step("---")
for (const COLOR of COLORS) {
rt(20)
set_color(COLOR)
sphere(5)
fd(7)
write_and_step(COLOR)
}
pd()
rt(20)
const NUMBER_OF_SIDES = COLORS.length
const ANGLE_TO_TURN = 360 / NUMBER_OF_SIDES
for (const COLOR of COLORS) {
set_color(COLOR)
fd(40)
lt(ANGLE_TO_TURN)
}