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

Error isn't helpful

Bummer! You didn't println the right values. You printed [January, Febuary, March] and we were looking for [January, February, March].

What is wrong here in my code?

months.swift
let months = [1, 2, 3]

for var i = 0; i < months.count; i++ {
    if i == 0 {
        println("January")
    } else if i == 1 {
        println("Febuary")
    } else if i == 2 {
        println("March")
    }
}

1 Answer

Stone Preston
Stone Preston
42,016 Points

The task states

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.

you did not use a for in loop, you used a traditional for loop. change your code to use a for in loop. you also need to start the loop at 1, not 0, since your numbers in the array start at 1.

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

For more information on for in loops, see the Swift eBook

Ah, I see (for-in loop missing).

Also, I wasn't spelling "February" correctly. :/

Thanks, Stone!