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

Pedro Marques
Pedro Marques
5,697 Points

Control Flow, If Statement Challenge (Swift)

Hey guys, I am stuck on this challenge and have no clue on what to do to fix the code. Can anyone help, please?

You are provided with a constant array named months that contains 3 consecutive numbers starting at 1. Using a for-in loop and an if statement, print out "January" when you encounter a 1, "February" when you encounter a 2, and finally "March" when you encounter a 3.

My code:

let months = [1, 2, 3]

for month in months {
    if month == 1 {
        println("January")
    } else if == 2 {
        println("February")
    } else {
        println("March")
    }
}

Thanks in advance!

2 Answers

Stone Preston
Stone Preston
42,016 Points

you are missing the variable name on your else if statement comparison. you have else if == 2 but you need to have else if month == 2.

let months = [1, 2, 3]
for month in months {
    if month == 1 {
        println("January")
    } else if month == 2 {
        println("February")
    } else {
        println("March")
    }
}

if you get compiler errors in the challenge, click the preview button and see what the error message says. Most of the time its pretty helpful. In this case it showed:

swift_lint.swift:11:15: error: unary operator cannot be separated from its operand
    } else if == 2 {

which told me exactly where the error was.

Pedro Marques
Pedro Marques
5,697 Points

Duh! You are absolutely right! Thanks a lot! =)