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

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,558 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
PLUS
Daria Rasheed
Courses Plus 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
}