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 trialJason Wayne
11,688 PointsIs there something wrong with the Generic Code Challenge (asking to write a duplicate with a single generic type.....) ?
We were asked to write a function named "duplicate" with a single generic type parameter T. The function takes 2 arguments, item of type T and numberOfTime of type Int, which returns an array of type T.
Below is my function, tested it on Playground, seems to be producing the output that the question is asking. Please do correct me if I'm wrong. Thanks!
func duplicate<T>(item: inout T, numberOfTimes: Int) -> [T] {
var theItemArray = [T]()
var count = 0
while count < numberOfTimes {
theItemArray.append(item)
count += 1
}
return theItemArray
}
var a = 5
let duplicateValue = duplicate(item: &a, numberOfTimes: 10)
1 Answer
Jason Anders
Treehouse Moderator 145,860 PointsHey Jason,
For this challenge, there are just two things that are preventing your code from passing.
- In the function declaration, you added
inout
for the 'item' argument. (Delete that). - Remove the ampersand (&) from the argument in the function call.
Fix that up and the rest is good to go. :)
Keep Coding!
Jason Wayne
11,688 PointsJason Wayne
11,688 PointsJason Anders - Thanks! It worked. I just dropped the
inout
.