iOS Closures in Swift First Class Functions Higher Order Functions

Taylor Smith
Taylor Smith
iOS Development Techdegree Graduate 14,152 Points

compiles perfectly in a playground...but I can't pass the challenge. what am I doing wrong? Closures Task 3 of 3

functions.swift
extension String {

    func transform(_ function: (String) -> String) -> String {

        return function(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)

1 Answer

Jhoan Arango
MOD
Jhoan Arango
Treehouse Moderator 12,180 Points

Hello :

Your string looks like "Hello World!" missing a comma.

Here is an example of what you could do:

extension String {

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

}

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

    for vowel in from.characters {

        if ["a","e","i","o","u"].contains(vowel){
            continue // If the vowel is in this array, it will continue the loop
        } else {
            output.characters.append(vowel) 
        }

    }
    return output
}

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

Good luck