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 trialEli MBS
1,158 PointsCan't find the mistake; Switch.swift
Would be very glad if someone could help me with this code. Technically, I gotta append the european cities to the european array, the asian cities to the asian array and the rest to otherCapitals :)
var europeanCapitals: [String] = []
var asianCapitals: [String] = []
var otherCapitals: [String] = []
let world = [
"BEL": "Brussels",
"LIE": "Vaduz",
"BGR": "Sofia",
"USA": "Washington D.C.",
"MEX": "Mexico City",
"BRA": "Brasilia",
"IND": "New Delhi",
"VNM": "Hanoi"]
for (key, value) in world {
// Enter your code below
switch key {
case "BEL": europeanCapitals.append("Brussels");
case "LIE": europeanCapitals.append("Liechtenstein");
case "BGR": europeanCapitals.append("Sofia")
case "VNM": asianCapitals.append("Hanoi")
default: otherCapitals.append(value)
// End code
}
}
1 Answer
Jason Anders
Treehouse Moderator 145,860 PointsHey Elias,
You're on the right track, except the challenge wants you to use the loop and variables to do the assigning. Right now you are hard-coding the values into the array (which can be done without a loop). Imagine if you had 5000 cities... would you want to hard-code every one into an array? Nope... so we use the loop.
As each loop iteration executes, there is a value in the key
variable and in the value
variable. Then it moves down to the switch. Here it will check it's key
value against the cases. When it finds a match, you can just append the value
variable to the correct array. So, for all the values, you will only need 3 case statements.
I've posted the correct code for the loop and switch
below for your reference. I hope it all makes sense.
for (key, value) in world {
// Enter your code below
switch key {
case "BEL", "LIE", "BGR": europeanCapitals.append(value)
case "IND", "VNM": asianCapitals.append(value)
default: otherCapitals.append(value)
}
}
Keep Coding!
Eli MBS
1,158 PointsEli MBS
1,158 PointsThanks! It helped me a lot. Now I got everything!