- Create an empty array
- Add a few elements to the end of the array
- Add an element to the beginning of the array
- Remove the last element from the array
- Remove the first element from the array
- Given an array containing the elements:
"orange","carrot","lemon","pomegranate","blood orange","apple"- Use the
indexOffunction to find the index of the elements of"carrot"and"apple"
- Use the
- Given the array
list = [], create a function which takes an argument,item, which should be added to thelist. After adding an element to thelist, print out the current elements in thelistto the console. - When we add elements to the
list, we need to make sure that these are strings. Improve your function so that it only adds to thelistif theitempassed to the function is a string.- For example,
myFunctionName('oranges')should return the elements inlist. However,myFunctionName(5)should return "Sorry, can only add words to the grocery list".
- For example,
- Create an empty array called
puppies. - Create a function called
addPuppy. The function takes one argument -puppy- and should add thatpuppyto the beginning of thepuppiesarray. For now, thepuppyparameter can be whatever you like (a string might be simplest).- For example, calling
addPuppy(puppy1), and immediately after callingaddPuppy(puppy2)should leave thepuppiesarray looking like this:[puppy2, puppy1].
- For example, calling
- Create another function called
adoptPuppy. This function does not take any arguments, but removes the last element in thepuppiesarray. The function should print out: "A puppy was adopted! There are now X puppies left to adopt", whereXis the total number of puppies in the array.
- Create an array with three elements, each element should be a string which represents a color.
- For example:
colors = ["red", "green", "blue"];
- For example:
- The array can only hold three arguments. Create a function which takes a
color(string) and anindex(number) of value0,1, or2. The function should swap out the element in thecolorsarray on the givenindexposition with thecolorpassed in.- For example, the call
swap("pink", 0)should result in the array["pink", "green", "blue"] - Add a guard clause to the function that verifies that the
indexis within the bounds (0-2) and that thecoloris a string.
- For example, the call
- Bonus: if the
indexis not passed through, pick a number within the bounds at random.