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

Vanessa Rodriguez
Vanessa Rodriguez
6,685 Points

Higher Order Functions

I am stuck and need help solving the rest of the challenge task. I was able to get past the first one, and I can probably find the solutions and copy and past but would like an explanation to get an understanding of the the next two challenge tasks and how they work?

Also, none of the solutions in the helper code referenced in the challenge work ): https://gist.github.com/Pasanpr/26e6e6227e62b7330a09a825c02a612f

functions.swift
// Enter your code below
extension String {
  func transform(_: (String) -> String) -> String {
    func returnString(_ name: String) -> String {
      return "My name is \(name)"
    }
    let name: String = "Vanessa"
    return returnString(name)
  }

  func removeVowels(from string: String) -> String {
    return String(string.characters.filter { !["a", "e", "i", "o", "u"].contains($0) })
  }

}

1 Answer

Jeff McDivitt
Jeff McDivitt
23,970 Points

There are multiple ways that you can complete this task. It has been a while since I did this one but this is how I did it this time:

  1. Inside your extension of String you only need one function that returns
  2. Inside your remove vowels function you have an argument as a String that returns a String. I made a for loop that goes through the characters that are not a vowel and appends them to the String inputted as an argument. You then return the output.
  3. Finally you call transform on the and assign it to a constant result

Let me know if that does not make sense :) Happy Coding!

extension String{

    func transform(_ argument : (String) -> String) -> String {
        return  argument(self)
    }
}

func removeVowels(from value: String) -> String {
    var output = ""

    for char in value.characters {
        if !(char == "a" || char == "A" || char == "e" || char == "e"
            || char == "i" || char == "I" || char == "o" || char == "O"
            || char == "u" || char == "U") {
            output.append(char)
        }
    }
    return output
}

let result = "Hello, World!".transform(removeVowels)