Learn
["Hip", "hip", "array!"]
We have learned about how to store groups of data into arrays in Swift. π
Here are some of the things that we covered:
- An array is a list of ordered items.var evenNumbers = [2, 4, 6, 8, 10]
- The first index in an array is 0.
- The
.count
property that returns the size of an array. - Some of the methods that come with arrays:
.append()
: adding an item to the end.insert()
: adding an item to an index.remove()
: removing an item from an index
- The
for
-in
loop can be used to iterate through an array.
Download the Arrays: Cheat Sheet for referencing the content covered in this lesson.
Instructions
1.
Optional: Write a Swift program to find the sum of even numbers and the product of odd numbers in an array.
For example, suppose we have an array that is [2, 4, 3, 6, 1, 9]
, then the program should output:
Sum of even numbers is 12 Product of odd numbers is 27
Take this course for free
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.