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 trial

iOS Swift Collections and Control Flow Control Flow With Conditional Statements Working With Switch Statements

Simon Feuster
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Simon Feuster
Full Stack JavaScript Techdegree Graduate 28,036 Points

For 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 } }

operators.swift
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
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi 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 and otherCapitals
  • 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! :sparkles:

Dan Lindsay
Dan Lindsay
39,611 Points

Hey 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
Dan Lindsay
39,611 Points

Ah, looks like Jennifer has given you good hints already! Can't see if someone posts an answer when you are writing one:)