Skip to content

Instantly share code, notes, and snippets.

@biglovisa
Created May 9, 2017 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save biglovisa/8f3d6622f2fdd29de9cd6f48a39946f3 to your computer and use it in GitHub Desktop.
Save biglovisa/8f3d6622f2fdd29de9cd6f48a39946f3 to your computer and use it in GitHub Desktop.
Array exercises

Array exercises

Foundations

  • 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"

Creating a shopping list

  • Given the array list = [], create a function which takes an argument, item, which should be added to the list. After adding an element to the list, print out the current elements in the list 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 the list if the item passed to the function is a string.
    • For example, myFunctionName('oranges') should return the elements in list. However, myFunctionName(5) should return "Sorry, can only add words to the grocery list".

Puppy adoption

  • Create an empty array called puppies.
  • Create a function called addPuppy. The function takes one argument - puppy - and should add that puppy to the beginning of the puppies array. For now, the puppy parameter can be whatever you like (a string might be simplest).
    • For example, calling addPuppy(puppy1), and immediately after calling addPuppy(puppy2) should leave the puppies array looking like this: [puppy2, puppy1].
  • Create another function called adoptPuppy. This function does not take any arguments, but removes the last element in the puppies array. The function should print out: "A puppy was adopted! There are now X puppies left to adopt", where X is the total number of puppies in the array.

Changing colors

  • Create an array with three elements, each element should be a string which represents a color.
    • For example: colors = ["red", "green", "blue"];
  • The array can only hold three arguments. Create a function which takes a color (string) and an index (number) of value 0,1, or 2. The function should swap out the element in the colors array on the given index position with the color 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 the color is a string.
  • Bonus: if the index is not passed through, pick a number within the bounds at random.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment