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 trialamber razzouk
1,391 PointsSwitch statements: Don't know where to even start
I'm overwhelmed and don't even know where to start here. I understand how to wrIte the switch statement but I'm lost on where to being and what I'm actually to do. HELP PLEASE!
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
}
1 Answer
Shade Wilson
9,002 PointsHi Amber,
What they want you to do here is to use a switch statement to sort the cities into their respective arrays: europeanCapitals
, asianCapitals
, and otherCapitals
. If you're not sure which countries go to which regions:
- LIE", "BGR", "BEL" are all european countries
- "IND", "VNM" are asian countries
- "USA", "MEX", "BRA" fall into the other capitals category
The basic syntax of a switch statement is Swift is
switch someValueToConsider {
case valueOne: //[response to valueOne]
case valueTwo: //[response to valueTwo]
default: //[response in the case that neither valueOne or valueTwo match someValueToConsider]
}
In this challenge, they're asking you to switch on the key of the dictionary world
, and to append the value when the key is matched. In case you forgot, the syntax for appending something to an array is
europeanCapitals.append(value)
Here's some code to get you started. Just take out the comments and plug in your own code!
for (key, value) in world {
// Enter your code below
switch key {
case // [european capital]: [append to europeanCapitals]
case // [asian capital]: [append to asianCapitals]
default: // [append to otherCapitals]
}
// End code
}