Skip to content

Instantly share code, notes, and snippets.

@biglovisa
Created May 9, 2017 19:53
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/e4ce059d5b218bd3592d6dc13738d45f to your computer and use it in GitHub Desktop.
Save biglovisa/e4ce059d5b218bd3592d6dc13738d45f to your computer and use it in GitHub Desktop.
iteration exercises

Iterating over collections

Summing integers

  • Given the array numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], iterate over the array and add all numbers together.

Multiplying and summing integers

  • Given the array numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], iterate over the array and multiply all the integers by themselves, and sum them together.

Filtering out odd numbers

  • Given the array numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], filter out all odd numbers.

Keeping short strings

  • Given the array words = ["cat", "hat", "rat", "pie", "luck", "mustard", "car", "plane", "wings", "i", "my", "no"];, iterate through the array and keep all words with three or fewer letters.

Keeping strings with an even number of letters

  • Given the string paragraph = "I am going to make an apple pie for the picnic this weekend. Hopefully no one is allergic to apples!", split the string on spaces so you get an array where each word of the paragraph is an element. Then, iterate through the array and keep all words with an even number of letters.
  • Do the same thing keepign all uneven words.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment