Application in CodeCraft
An array of all red materials
In this example we use an array to store all materials that are red, then build a column with those materials.
// Red blocks
let redBlocks = ['box_red', 'linen_red', 'redstone'];
for (let i = 0; i < redBlocks.length; i++) {
block_m(0, i + 1, -10, redBlocks[i]);
}
Array of arrays
An element in an array can itself be an array. In this example, we created an array called blocks
that stores arrays containing the coordinates and materials of various blocks in this format: [x, y, z, m]
blocks = [[2, 4, -9, 56], [2, 2, -9, 56], [3, 3, -10, 59], [4, 2, -9, 56], [4, 4, -9, 56]]
for (let b of blocks) {
block_m(b[0], b[1], b[2], b[3]);
}
See the picture below. We built 4 red blocks and a yellow block in the middle. Their location and material values [x, y, z, m]
are stored in blocks
as five number arrays. Variable b
in the for
loop represents each array within blocks
.