Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Closures in Swift First Class Functions Higher Order Functions

Carlos Medina
Carlos Medina
2,724 Points

The "removeVowels" task doesn't work, there's no error shown on the "Preview" tab.

Challenge Task 2:

I added my own code but it failed, then I started using the solutions posted by others but it fails as well.

Trying to see the compiler errors on "Preview" shows nothing.

boi
boi
9,269 Points

Hey Carlos!! Could you please post your code, That would be the only way to identify the problem

Carlos Medina
Carlos Medina
2,724 Points

The error is: Bummer: Make sure removeVowels accepts a String and returns a String

But, in the code you can see that removeVowels accepts and returns a String.

Full code:

extension String {
  func transform(_ function: (_ string: String) -> String) -> String {
    return function(self)
  }

  func removeVowels(from string: String) -> String {
    let vowels: [Character] = ["a", "e", "i", "o", "u"]
    var newString: String = String()

    for character in string.characters {
      if !vowels.contains(character) {
        newString.append(character)
      }
    }

    return newString
  }
}

1 Answer

Can you link the challenge?

Looking at the code it doesn't really make sense - if the function is inside an extension then it shouldn't be taking an input unless that input is to modify the string it's being called on.

I've just modified it to the below to what I feel makes sense, and it seems to be tested and working in playgrounds - but naturally with these codes challenges they sometimes need to be catered for slightly.

extension String {
  func transform(_ function: (_ string: String) -> String) -> String {
    return function(self)
  }

  func removeVowels() -> String {
    let vowels: [Character] = ["a", "e", "i", "o", "u"]
    var newString: String = String()

    for character in self {
      if !vowels.contains(character) {
        newString.append(character)
      }
    }

    return newString
  }
}

let new = "aeiou and voila"

let withoutVowels = new.removeVowels()```