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 trialMatt Conway
1,572 Pointsswitch
Not sure why this isn't working for me .....
Challenge Task 1 of 1
Here we have a dictionary of type [String: String] 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, and otherCapitals. 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.
I've already set up the for in loop for you so jump right in!
/Users/mattconway/Desktop/Screen Shot 2016-01-15 at 5.56.34 PM.png
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"]
let value = world
for (key, value) in world {
// Enter your code below
switch value {
case "BEL", "LIE", "BGR": print("European Capitals")
case "IND", "VNM": print("Asian Capitals")
default: print("Other Capitals")
}
// End code
}
2 Answers
Steven Deutsch
21,046 PointsHey Matt Conway,
The challenge asks you to append the matching values for each key to their appropriate arrays. The first problem, you're switching on the value and not the key. You need to switch on the keys and then for each key that matches the specific case, the code for that statement will be executed. The second problem is that this statement should be appending the value to the appropriate array, you're printing a string when a match occurs.
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 {
// Enter your code below
switch key {
case "BEL", "LIE", "BGR": europeanCapitals.append(value)
case "IND", "VNM": asianCapitals.append(value)
default: otherCapitals.append(value)
}
// End code
}
Good Luck!
Matt Conway
1,572 PointsYeah, misread instructions, printed a string instead of appending the value