Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Basics (retired) Control Flow If Statement

Vincent Smithers
Vincent Smithers
2,543 Points

Arrays/ For-IN/If-then Assignment

I have been stuck on this task for over a day. The video does not seem to give insight on how to solve this problem, and the editor will not tell me what I am doing wrong. I have looked elsewhere to find the solution, and I have been working it out on Xcode

Finally I have seemed to get the correct output while using Xcode. The console returns January, February and March. When I copy and the paste the code into the treehouse editor the only response I get is 'Bummer Dude!' with no explanation as to why the code is not accepted. Here is the what I have:

let months = [1, 2, 3]

for month in months{
    if month == months[0]{
        println("January")
    } else if month == months[1]{
        print("February")
    } else{
        println("March")
    }
}


let months = [1, 2, 3]

for month in months{
    if month == months[0]{
        println("January")
    } else if month == months[1]{
        print("February")
    } else{
        println("March")
    }
}

4 Answers

Hi Vincent,

This is a fiddly challenge and one that, in reality, you'd never replicate code-wise as the result is horribly clunky.

The result shoudl look like:

let 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")
  }
}

So, we've got an array and we're looping through that with the for month in months statement.

This presents us with each individual month in turn as it iterates through the array. So, it is first 1 then 2 then 3. The loop then ends.

What we need to do is to see what each presented month is and output the appropriate string.

So the first iteration, month is 1 - so the nasty nested if statement says if (month == 1) output January. And so on.

I hope that clears it up for you. And sorry for leaping straight to the solution but I don't think you should dwell on this for ages. The concept is key - this example is a bit ugly.

Steve.

Yep - OK.

We're using the content of the array, not the index. So, in this array month[0] the first element is 1. The secong element, month[1] is 2. These could equally be any other value but we've got an array of integers.

The loop iterates through each item without expressly calling the index. So, it loops first to month[0] and sees if the contents of month[0] are equal to the value of 1.

On each loop, the object held by the month variable is the content of each element of the array, first pass it is month[0], on the second pass, month holds month[1] etc. There's no counter numbering convention here; we just never use the [x] method of accessing the array.

I hope that makes sense.

Steve.

Vincent Smithers
Vincent Smithers
2,543 Points

Thanks Steve!

I guess I am not understanding why when referring to an item in the array for the if-else statement in this example, it is not 0,1, and 2, but is 1,2 and 3. This seems to counter what was taught in the videos about numbering items in an array. I also do not understand why my solution would work in X-code and not in the editor. Anyways, thanks for clearing that up!

Vincent Smithers
Vincent Smithers
2,543 Points

That does make sense when you explain it in that way, but I would have never guessed that was the problem. Thank Again!