iOS Closures in Swift First Class Functions Higher Order Functions

Edwin Carrasquillo
Edwin Carrasquillo
10,315 Points

Isn't the removeVowels function accepting a String and returning a String?

The checker is telling me that removeVowels must accept a String and return a String, and as far as I can tell the function is doing that?

If I'm missing something here I'd appreciate a pointer.

functions.swift
extension String {
    func transform (_ transform: (String) -> String) -> String {
        return transform(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
    }
}

2 Answers

andren
andren
28,384 Points

While the instructions are not super clear on this, the removeVowel function is not meant to be an extension of String. It should be a stand alone function, which in task 3 is going to be passed to the transform function you create in task 1.

So the issue is the placement of your function, not the function code itself. If you move it like this:

extension String {
    func transform (_ transform: (String) -> String) -> String {
        return transform(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
}

Then your solution will be accepted.

Daria Rasheed
PRO
Daria Rasheed
Pro Student 1,887 Points

Agree with @andern, the instructions were not super clear, I have always seen challenges with example call and return value which is expected by the test examiner, but here even that was missing, anyways here is what worked for me.

func removeVowels(from str: String) -> String {
    var result = ""
    let vowels: [Character] = ["a","e","i","o","u", "A", "E", "I", "O", "U"]
    for c in str.characters {
        if !vowels.contains(c) {
            result.append(c)
        }
    }
    return result
}