Demo Array Basics in CodeCraft
Since we know how to loop through an array, we can build structures in CodeCraft to demonstrate some array basics such as indexing, and changing an item's value.
Original Array
First, build white columns representing the original number array:
// Original array, box_white, 15
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for (let i in arr) {
column_m(i, -20, arr[i], 15);
}
Demo 1: access items at index number 2 and 4, highlight them blue and yellow
column_m(2, -20, arr[2], 45); // blue
column_m(4, -20, arr[4], 59); // yellow
Demo 2: change item's value
arr[7] = 5
column_m(7, -20, arr[7], 55); // purple
The item at index 7
is assigned a new value, so the new column(purple, height:5) is shorter than the original column(white, height:8)