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 trialOmer Celik
Courses Plus Student 1,722 PointsHello, I am kinda stuck here. I have tried couple things but I can't pass it. Could you help me with this please. Thanks
I don't know how to add case result to the array.
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 world {
case "LIE", "BEL", "BGR": europeanCapitals.append(world)
case "VNM", "BRA", "IND": asianCapitals.append(world)
default: otherCapitals.append(world)
}
// End code
}
3 Answers
Charles Kenney
15,604 PointsOmer,
In order to map the cities (values) to the correct capitals array you need to switch on the country codes (keys). You made the mistake of switch on the entire object. Otherwise, your code should work well. This was my solution.
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", "BGR", "LIE":
europeanCapitals.append(value)
case "IND", "VNM":
asianCapitals.append(value)
default:
otherCapitals.append(value)
}
// End code
}
Hope this helps,
Charles
Omer Celik
Courses Plus Student 1,722 PointsThank you very much for quick response I finally got it but I have one more question. Why did you use break
after first case?
Charles Kenney
15,604 PointsOmer, you are very welcome. As for that break, I honestly don't know why I added it, (it is not necessary) I am just very tired at the moment and made a mistake. Lol, good catch!
Jeff McDivitt
23,970 PointsHi Omer -
- You are switching on the wrong value, you should switch on the key
- Check you case values
- You are not appending world you are appending value
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","BGR","LIE":
europeanCapitals.append(value)
case "IND" :
asianCapitals.append(value)
default:
otherCapitals.append(value)
}
}
Bernard Juru
3,038 Pointsswitch (key) { case "BEL", "BGR", "LIE": europeanCapitals.append(value) case "IND", "VNM": asianCapitals.append(value) default: otherCapitals.append(value) }
this works
Omer Celik
Courses Plus Student 1,722 PointsOmer Celik
Courses Plus Student 1,722 PointsIn the editor we have a dictionary that contains a three letter country code as a key and that country's capital city as the associated value.
We also have three empty arrays,
europeanCapitals
,asianCapitals
, andotherCapitals
. The goal is to iterate through the dictionary and end up with just the names of the capital cities in the relevant array.For example, after you execute the code you write,
europeanCapitals
will have the values["Vaduz", "Brussels", "Sofia"]
(not necessarily in that order).To do this you're going to use a switch statement and switch on the key. For cases where the key is a European country, append the value (not the key!) to the europeanCapitals array. For keys that are Asian countries, append the value to asianCapitals and finally for the default case, append the values to otherCapitals.