- 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
indexOf
function 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 thelist
to 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 thelist
if theitem
passed 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 thatpuppy
to the beginning of thepuppies
array. For now, thepuppy
parameter can be whatever you like (a string might be simplest).- For example, calling
addPuppy(puppy1)
, and immediately after callingaddPuppy(puppy2)
should leave thepuppies
array 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 thepuppies
array. The function should print out: "A puppy was adopted! There are now X puppies left to adopt", whereX
is 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 thecolors
array on the givenindex
position with thecolor
passed 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
index
is within the bounds (0-2) and that thecolor
is a string.
- For example, the call
- Bonus: if the
index
is not passed through, pick a number within the bounds at random.