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 trialDenali Lord
Courses Plus Student 1,955 PointsHelp on 1st challenge question of combining switch statements and ranges, etc.
I am stuck on the first challenge question of control flow for swift.
I am not sure about the following things:
- Syntax/ format is correct
- Where would I use the constant, let World?
- for the asianCapitals, how would I include that in the switch statement.
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
for europeanCapital in
europeanCapitals {
switch europeanCapital {
case "BEL":
print("Brussels")
case "BGR":
print("Sofia")
case
default:
// End code
}
1 Answer
Tobias Helmrich
31,603 PointsHey there,
you don't have to write a second loop inside of the existing loop and note that the europeanCapitals
array is empty in the beginning because you have to fill it so the loop wouldn't even execute.
To solve this challenge you just need a switch statement with all the different cases and note that you can group cases by separating them with a comma. In the switch statement you only need two cases (for european capitals and asian capitals) and one default case (for other capitals).
In the cases you shouldn't print out the city names but add them to the appropriate array (europeanCapitals
, asianCapitals
or otherCapitals
) and also take care that you don't hardcode the cities but that you use the value
constant you get when you loop over the world
dictionary.
Like so:
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
}
I hope that helps, if I didn't answer all of your questions or if you have further questions feel free to ask! :)
Denali Lord
Courses Plus Student 1,955 PointsDenali Lord
Courses Plus Student 1,955 PointsHi Tobias,
Sorry for the delay in response. Thank you very much and yes you answered my questions well!