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
Danny Kilkenny
4,507 PointsHelp Needed With First Closures Challenge Part 2 of 2
I am very confused as to why my code isn't correctly solving the Closures Challenge.
Here is the prompt: Assign the addTwo function we just created to a constant named addition.
Call the function using the constant and pass in an argument. Assign the resulting value to a constant named result.
func addTwo(x: Int) -> Int {
return 2 + x
}
let addition = addTwo
addition(x: 6)
let result = addition
Could someone please help with this?
3 Answers
Lamar Greene
9,581 PointsI don't recall taking this course yet, but hopefully this is the answer...
I think the confusion lies in the last line of the directions. When you call
addition(x: 6)
that is all you are doing. In the next line, where you assign addition to the constant result, you are essentially saying result is also ANOTHER constant for the function addTwo().
I believe you need to have result be the constant of the actual call to addition, with the argument passed. like so...
func addTwo(x: Int) -> Int {
return x + 2
}
let addition = addTwo
let result = addition(6)
Danny Kilkenny
4,507 PointsThank you so much!!!
Adam Mazurick
Courses Plus Student 7,653 Pointsfunc addTwo(to: Int) -> Int { return to + 2 }
let addition = addTwo
let result = addition(6)
Danny Kilkenny
4,507 PointsDanny Kilkenny
4,507 PointsYeah I tried that too cause that's what I originally had but it still said "Make sure you assign the result of calling addTwo to the constant result"
Lamar Greene
9,581 PointsLamar Greene
9,581 Pointscan you provide a link to the challenge?
Danny Kilkenny
4,507 PointsDanny Kilkenny
4,507 PointsSure! The solution to part 1 is just the extension without any of the function code. https://teamtreehouse.com/library/closures-in-swift-2/first-class-functions/functions-as-first-class-citizens
Lamar Greene
9,581 PointsLamar Greene
9,581 PointsIt looks to be a syntax issue. I have edited my answer to reflect... Calling addition with the x: in the argument was throwing the error.