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 trialJUNSEI TEI
2,055 PointsCode Challenge in Collection and Control Flow
Help me! I get stuck here.
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 {
switch world {
case "BEL": "Brussels", "LIE": "Vaduz", "BGR": "Sofia" : europeanCapitals.append(value)
case "IND": "New Delhi", "VNM": "Haoni" : asianCapitals.append(value)
case "USA": "Washington D.C.", "MEX": "Mexica City", "BRA": "Brasilia" : otherCapitals.append(value)
}
// End code
}
2 Answers
Steven Deutsch
21,046 PointsHey Junsei Tei,
There are a few things we need to correct in your code. First, your switch statement needs to switch on the key value, not world. So you only have to write out the keys in your case statements. Second, you should use a default statement to capture the rest of the remaining keys. This way you don't have to list what those specific keys are. All the values that weren't appended to europeanCapitals or asianCapitals will be appended to otherCapitals by default.
Your code should look like this:
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 {
switch key {
case "BEL", "LIE", "BGR": europeanCapitals.append(value)
case "IND", "VNM": asianCapitals.append(value)
default: otherCapitals.append(value)
}
// End code
}
Hope this helps! Good luck!
Emil Rais
26,873 PointsYou're switching on the world dictionary, but that is a dictionary of country codes and capitals. What you're interested in is switching on the country codes. As you iterate through the for loop the country codes will be stored in the constant named key.
JUNSEI TEI
2,055 PointsAppreciate for your help!
JUNSEI TEI
2,055 PointsJUNSEI TEI
2,055 PointsAppreciate for your help!!