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 Generics in Swift Generic Functions, Parameters and Constraints Generic Functions

Ingo Ngoyama
Ingo Ngoyama
4,882 Points

My code works but it wont let me pass.

I have put in all the correct arguments and labels and my output is matches so why is my code not working

func duplicate<T>( _ item: inout T, _ numberOfTimes: inout Int) -> [ T ]
    var anArray = [item]
    var counter = 0
        counter += 1
    } while counter < numberOfTimes
    return anArray

var a = 1
var b = 3

var myArray = duplicate(&a, numberOfTimes: &b)

2 Answers

Jeff McDivitt
Jeff McDivitt
23,970 Points

Although there are multiple ways to complete this. Here is the way that I did. There are several things that you are including that do not need to be included

func duplicate<T>(item:T, numberOfTimes:Int) -> Array<T> {
    var newArray: [T] = []
    for _ in 0..<numberOfTimes {
    return newArray
duplicate(item: 1, numberOfTimes: 4)
Christoph Eck
Christoph Eck
Courses Plus Student 7,021 Points

Short and working

func duplicate<T>(item: T, numberOfTimes: Int) -> [T] {
    var ary = [T](repeating: item, count: numberOfTimes)
    return ary