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 trialPaul Cullen
1,414 PointsEditor not working right?
Hey, ive written 2 versions of this code, both come up with the error "You must use an if statement" however they compile fine in xcode, both iterations are below, can you see an issue?
let months = [1, 2, 3]
for month in months { if(month == 1) { println("January"); } if(month == 2) { println("February"); } if(month == 3) { println("March"); } }
for i in months { if(i == 1) { println("January"); } if(i == 2) { println("February"); } if(i == 3) { println("March"); } }
let months = [1, 2, 3]
for i in months {
if(i == 1) {
println("January");
}
if(i == 2) {
println("February");
}
if(i == 3) {
println("March");
}
}
3 Answers
Steve Hunter
57,712 PointsHi Paul,
Whislt I don't think it would cause a problem, your code doesn't need the semi-colons at the end of each line. However, this code works:
let months = [1, 2, 3]
for month in months {
if month == 1 {
println("January")
}
if month == 2 {
println("February")
}
if month == 3 {
println("March")
}
}
The compiler was playing up intermittently yesterday - it wanted incorrect code entering first (add a couple of incorrect brackets to your code) before it would pass the subsequently corrected code. I don't know if that persists today.
Steve.
Paul Cullen
1,414 PointsThats some old habits from my PHP syntax, will need to try and forget about using them. Thanks Steve.
Steve Hunter
57,712 PointsI always use too many code ornaments like that - it just reads more easily to my little brain!
Paul Cullen
1,414 Pointsha! glad im not the only one
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsIt seems to be the brackets around your conditional tests that are failing the compiler tests. Not sure I agree with that - it shouldn't make a difference.
EDIT No it isn't - it is the lack of space between the
if
keyword and the bracket. Again, not sure I agree with that!!