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 trialManuel Stark
3,110 PointsNot sure what they're asking me here. A little help?
Your task is to extend the String type and add a method that lets you add an integer value to the string. For example: "1".add(2) If the operation is possible, return an integer value. If the string does not contain an integer and you cannot add to it, return nil.
// Enter your code below
extension String {
var add: Int {
if return {
}
else
{ return nil }
}
}
2 Answers
Tobias Helmrich
31,603 PointsHey Manuel,
note that you have to extend the String type with an add
method instead of a computed property.
One way you could implement this function is to use a guard
statement where you try to change the string you're using the method on to an integer. If this doesn't work you can return nil
, otherwise you can just add the passed number
to the converted number and return the result.
Like so:
// Enter your code below
extension String {
func add(number: Int) -> Int? {
guard let convertedNumber = Int(self) else {
return nil
}
return convertedNumber + number
}
}
I hope that helps, good luck! :)
Joshua Hudson
5,237 PointsThanks for the assist Tobias!
Sadly Im having to look up almost every one of Gabe's challenges as the videos will present something like "1 +1 = 2 and 2 + 2 = 4" and then the challenges read "Please calculate the mass of the sun". I'm finding it very difficult to learn from him as opposed to Pasan, whose challenges are a logical extension of the subject matter.
Sean Fillmore
12,458 PointsI made this complaint as well, the code challenges have little if anything to do with the videos.
Manuel Stark
3,110 PointsManuel Stark
3,110 PointsHey, thank you Tobias! That was extremely helpful. It's a bit ironic how the world works. I was on your website this morning after you helped with a previous problem I had and now here you are helping me again. I appreciate your help!
Tobias Helmrich
31,603 PointsTobias Helmrich
31,603 PointsHaha, glad to hear that! I'm always happy to help! :)