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 trialMiguel Correia
1,216 PointsNeed some explanation
I need some explanation in this one guys, please don't tell me the answer just explain it to me because I have been in this exercise for over an hour and I really want to go thought without cheating. Thank you guys.
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
// End code
}
2 Answers
Jhoan Arango
14,575 PointsHello:
So all you have to do is a switch, that will switch to key, and on each case you will then append value to it. Make sure that the default value is one of them.
switch _ {
case "" :
// Do something
case "" :
// Do something
default:
// Do whatever was left
}
This will guide you a bit to your answer. Hope it helps
If you need more help please let me know. I admire then fact that you want to get the answer on your own without "cheating".
Good luck
David Anguiano
Courses Plus Student 9,978 Pointsvar 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("Vaduz") case "BGR": europeanCapitals.append("Sofia") case "USA": otherCapitals.append("Washington D.C.") case "MEX": otherCapitals.append("Mexico City") case "BRA": otherCapitals.append("Brasilia") case "IND": asianCapitals.append("New Delhi") case "VMN": asianCapitals.append("Hanoi") default: asianCapitals.append(value) } // End code }
Miguel Correia
1,216 PointsMiguel Correia
1,216 PointsThis is what I came with, is any of this ones right ?
Or this one
Jhoan Arango
14,575 PointsJhoan Arango
14,575 PointsHey Miguel:
Well, you are close, you are printing instead of appending the values to the arrays. Also you do not switch on the capital arrays, you have to switch on key then append value to these arrays.
The for in loop is iterating through the dictionary world, and giving you back each key and value, so when you switch on key, you will get in return a value. This value needs to be added to the corresponding array.
I'm not sure if you are doing these challenges on your Xcode, but if not, then open a playground and do them there, you will get better results. Then you can copy and paste your code and pass your challenges.
Good luck