Write a sketch that prints a list of random fruit.
Create an array of names of fruit. Name at least 6 fruit.
var fruitNames = ["Orange", "Apple"]; // not enough fruit
Use text() to display the output on the screen as a vertical list of fruit names. Print 20 random fruit names.
Use the length of the fruitNames array to determine how many fruit are in your array. So you are going to use the random function to generate a random number between the beginning and end of the array. Remember that when you give random() a range of numbers, it returns a random number from the lower number up to but not including the upper number. So random(1,6) will return random numbers between 1 and 5 but not 6.
Remember that the first element in the array starts at 0.
You must use the int() function to turn the random number into an integer before using the number to index the array. Otherwise you will get an error.
Thus, if an additional fruit name is added to the fruitNames variable and NO OTHER CHANGES are made to your code, it should still work and have a chance of displaying the newly added fruit names.
This is an important design decision so that the data in your code can change without having to write new code to deal with it.
Testing note: Make sure you see the first and last fruit names in your array. You may not see them every time due to random numbers. But if you never see the last name, there is either something wrong with your code or you are very unlucky.
Go back to Arrays 1