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

loops

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.

months.swift
let months = [1, 2, 3]
for month in months {
if months == 1 {
println("January")
}
else if months == 2 {
println("February")
}
else months == 3 {
println("March")
}
}
Lukas Smith
Lukas Smith
4,026 Points

and the question is ?

This is the question : 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.

let months = [1, 2, 3]

2 Answers

Lukas Smith
Lukas Smith
4,026 Points

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

}

Thanks worked

Michael Hulet
Michael Hulet
47,913 Points

Your code fails because in the if and else if statements, you're checking the months variable (which contains the array of all the numbers) to see if it's equal to one specific number. Instead, you need to check the month variable, which will update for each iteration of the loop. Another problem is that else blocks can't contain conditions. You have to either remove the condition or add an if. This should work:

let months = [1, 2, 3]
for month in months {
  //Notice the subtle-but-important omission of the "s" here...
  if month == 1 {
    println("January")
  }
  //...and here
  else if month == 2 {
    println("February")
  }
  //Lastly, notice the "if" here, and the singular month
  else if month == 3 {
    println("March")
  }
}

Can you please take a look at it again because it didn't work

Michael Hulet
Michael Hulet
47,913 Points

Noticed one other problem, just fixed it

Anthony Babich
Anthony Babich
5,505 Points

If you noticed, I'm pretty sure the only reason this one didn't work is the "else if months" instead of the singular month in the final else if statement.

Michael Hulet
Michael Hulet
47,913 Points

Make that problem #3, then. I must be really off my game today. I swear this should work now