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 trialMichael Messner
Courses Plus Student 905 Pointsloops
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]
for month in months {
if months == 1 {
println("January")
}
else if months == 2 {
println("February")
}
else months == 3 {
println("March")
}
}
Michael Messner
Courses Plus Student 905 PointsThis 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
4,026 Pointsvar 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") }
}
Michael Messner
Courses Plus Student 905 PointsThanks worked
Lukas Smith
4,026 Pointsno probs
Michael Hulet
47,913 PointsYour 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")
}
}
Michael Messner
Courses Plus Student 905 PointsCan you please take a look at it again because it didn't work
Michael Hulet
47,913 PointsNoticed one other problem, just fixed it
Anthony Babich
5,505 PointsIf 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
47,913 PointsMake that problem #3, then. I must be really off my game today. I swear this should work now
Lukas Smith
4,026 PointsLukas Smith
4,026 Pointsand the question is ?