NOTE: Learners are introduced to arrays in the Asteroid Belt.
Variables store information, or values, in a program. There are different types of variables that are used in different situations: strings, integers, and arrays.
Strings are groups of characters, like words or phrases, and integers are whole numbers, like 5, 50, or 500. But what if we want to store multiple values in a variable? The solution is an Array!
An array is an ordered list of related items that can include any type of value, including Strings and Integers.
In real life, you can think of your closet as an array- you use your closet to organize your clothes and keep them in a specific order. The order you hang your clothes in organizes them in a defined way- your shirts are going to stay where you hang them, and you remove a shirt, the order of the rest of the shirts won't be changed.
Each value in an array has an index number, which marks the exact position of the value in the array. This index is critical for finding and storing values, since an array can have hundreds or thousands of items!
The first value in an array has an index of 0. If your yellow shirt is the first item in your closet, it will be at located at index 0.
In Kodable, learners practice using arrays in two ways: in maze levels and an asteroid blaster game.
1. Arrays in the maze:
In order for our fuzz to reach the end of the maze, it needs to be able to roll through one or multiple tunnels. If a tunnel are closed, we need to find a key that will open it. Try coding the fuzz to roll through the maze and pick up a key along the way.
In Array practice levels, the tunnel will need a short list of keys to open it. The order of the list items matters. If the tunnel shows diamond 1st, triangle 2nd, the learner will need to code the fuzz to collect the keys in the same order. Any keys collected will be stored in the array brackets at the top of the screen. If the array of keys is equal to the array of key holes on the tunnel, then the door will open and the fuzz will roll on through!
2. Arrays in the asteroid-blaster game
Arrays in the world of Asteroidia work like they do in programming. Learners can use an array to store multiple values, achieving three moves with just one command. This allows students to solve some levels in just one move.
In this lesson, the colors represent string variables and the numbers represent integers. There are two string values (red and yellow) and one integer value (2). I need to correctly order my list of variables in the command bin to be able to clear the asteroid field when I match the values: First, yellow needs to clear. Then, 2. Finally, red. The values must be stored in the variable in the correct sequence for the learner to successfully clear the asteroid field.
Why We Use Arrays: Arrays make storing and finding values easier and efficient. Programmers can use one command to search all of the values stored in one array, without needing to know what each variable may be.