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 trialChristian Dangaard
3,378 PointsHigher Order Functions Code Challenge - My Brain Hurts
For Code Challenge 1 of 2, I somehow, despite still trying to understand the function, got my code accepted. Code are as follows:
func differenceBetweenNumbers(a: Int, b:Int) -> (Int) {
return a - b
}
func mathOperation(differenceBetweenNumbers: (Int, Int) -> Int, a: Int, b: Int) -> (Int) {
return differenceBetweenNumbers(a, b)
}
For Challenge 2 of 2, my accepted code is:
let difference = mathOperation(differenceBetweenNumbers, 10, 3)
However, 2 things to note:
- For this to be accepted, I had to remove the () from the (Int) in the return type from the func mathOperations
- Xcode (7.2) doesn't like "let difference = mathOperation(differenceBetweenNumbers, 10, 3)" and gives me a "Missing argument labels
a:b:
in call
So painful, my apologies for my whinging, I'll get a ton more practice under my belt.
Can someone please help me here, thank you in advance.
3 Answers
Steven Deutsch
21,046 PointsHey Christian Dangaard,
The code as you've written it should not pass without omitting the external parameter names for your math operation function. This is why Xcode is throwing the error you described. Also, I changed the first parameter name from differenceBetweenNumbers to mathOp. You don't want to return a call of differenceBetweenNumbers in your function, but rather any math operation you pass in.
func differenceBetweenNumbers(a: Int, b:Int) -> (Int) {
return a - b
}
// Enter your code below
// Notice how I've added underscores before the parameters a and b
func mathOperation(mathOp: (Int, Int) -> Int, _ a: Int, _ b: Int) -> Int {
return mathOp(a, b)
}
let difference = mathOperation(differenceBetweenNumbers, 10, 3)
Good Luck!
Christian Dangaard
3,378 PointsThanks, much appreciated. I've gone through every question in the forum against the "Capturing Variables" video which helped greatly also.
Carlos Freire
2,289 PointsThis makes no sense given what we were shown in the previous video.
Some of these topics are taught in a horrible manner.
Steven Deutsch
21,046 PointsHey Carlos,
You should check out the course on Closures with Swift 2. This is a video from the Swift 1 track. Closures are a higher level concept in Swift. It took me awhile to grasp their usefulness, capabilities, and understand all the short hand syntax. I really recommend reading the Apple's Swift Language Guide's chapter on Closures. It's the best resource on the topic.
Good Luck
Carlos Freire
2,289 PointsHi Steven,
Thanks for the quick reply. I have 0 programming experience. I'm doing this from scratch. I thought it could be a good idea to take this course and then jump into the swift 2.0 course with a bit more knowledge to really grasp the topics. Do you think this is a good idea? Or would you go directly into 2.0?
Steven Deutsch
21,046 PointsThis course won't hurt you, however, Swift is on version 2.2 of the language. This means that some features in Swift 1 may be deprecated and some of the syntax of the language has changed. You are probably most familiar with println() being renamed to print(). I would suggest moving to the Swift 2.2 course as it is the most recent version of the language. You can then use the Swift 1.0 track to supplement that knowledge. This Fall Swift will be upgrading to version 3.0 and there will be quite a bit more changes, however, nothing that you can't account for or solve.
Marina Alenskaja
9,320 PointsMarina Alenskaja
9,320 PointsHi Christian
This one was tricky for me too. But what exactly is your question? Is it why xcode gives an error? Or explaining of the challenge in general?