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.

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