Make an RPG
Arrays are used to store multiple values in one variable.
Here is an example:
Of course, you could create separate variables for them like this:
But, then you can't use loops to loop over all of them.
This is how you access the array:
Try the above code and see if you can guess which monster appears.
Let's check the length of the array:
What is the length of the array?
The array length will always be 1 greater than the highest element.
Add the following before the print statement
What's the length of the array now?
Now, we're going to loop through our array: Add the following code and watch what happens to the variable x.
Now, output the monster in each position
Now, let's add a monster to the end of the list.
The push method will add a variable to the end of an array.
Get the Latest Tutorial Updates