Learn

What happens if we want to iterate through items of an array? We can use a for-in loop!

The syntax looks like the following:

for item in array { // Loop body }

For example, suppose we have a String array that looks like this:

var employees = ["Michael", "Dwight", "Jim", "Pam", "Andy"]

We can write a for-in loop like so:

for item in employees { print(item) } // Prints "Michael" // Prints "Dwight" // Prints "Jim" // Prints "Pam" // Prints "Andy"

This is a powerful tool for working with and manipulating a large amount of data.

Instructions

1.

Here are the orders from a receipt. Let’s do some calculations.

First, create a variable called total of type Double and give it a value of 0.0.

2.

Iterate through receipt using a for-in loop and call the iterator variable item.

And add each item to total.

3.

There’s an 8.875% sales tax in New York.

Change the value of total by multiplying itself by 1.08875.

4.

Finally, print the value of total.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?