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 trialSimon Feuster
Full Stack JavaScript Techdegree Graduate 28,036 PointsFor Loop and switch doesn't work
I don't know what i have to do here. Tried the following code but it is not working.
for (key, value) in world { // Enter your code below switch key { case "BEL","LIE","BGR": europeanCapitals.append(world[key]) case "USA","MEX","BRA": europeanCapitals.append(world[key]) default: otherCapitals.append(world[key]) // End code } }
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(world[key])
case "USA","MEX","BRA": europeanCapitals.append(world[key])
default: otherCapitals.append(world[key])
// End code
}
}
2 Answers
Jennifer Nordell
Treehouse TeacherHi there! You're pretty close, so I'm going to give some pointers here.
- You should be appending the
value
- Your code only appends to
europeanCapitals
andotherCapitals
- Currently Washington, D.C., Mexico City, and Brasilia are being appended to the
europeanCapitals
- You lack any code that appends to the
asianCapitals
Give it another shot with these hints in mind!
Dan Lindsay
39,611 PointsHey Simon,
You are close on this one. You definitely want to do the switch on the key like you are already doing, but you haven't used the value variable yet. Instead of append(world[key]), you just want to append(value), as we don't want our arrays filled with the keys. Also, it looks like you are using the europeanCapitals twice. If you change your second case to otherCapitals.append(value) and change the default to asianCapitals.append(value), your code should pass. Hope this helps!
Dan
Dan Lindsay
39,611 PointsAh, looks like Jennifer has given you good hints already! Can't see if someone posts an answer when you are writing one:)