Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift Basics (retired) Control Flow If Statement

Michael Watson
Michael Watson
1,822 Points

I'm getting an error saying "I need to use an if statement" on this specific question. However, I am!

Is there something I'm missing here? I have an if statement included and yet I can't get this challenge passed. Is there another way I should be approaching this?

months.swift
let months = [1, 2, 3]
for i in 0...2 {

    if(months[i] == 1) {
        print("January")
    }
    else if(months[i] == 2) {
        print("February")
    } else {
        print("March")
    }

}

1 Answer

Kevin D
Kevin D
8,646 Points
  1. You should replace the '0...2' with the let variable 'months' in your for in loop
  2. You don't need to specify months[i] in the if statement, you can just write the 'i' variable
  3. It should be println() instead of print()
  4. I think they wanted you to use an else if statement for the i == 3 too

The completed code should look like this:

let months = [1, 2, 3]

for i in months {

    if (i == 1) {
        println("January")
    } else if (i == 2) {
        println("February")
    } else if (i == 3) {
        println("March")
    }

}