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 Closures in Swift First Class Functions Higher Order Functions

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
14,242 Points

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

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()```