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 trialTony Luo
14,224 PointsHigher Order Functions - Why isn't my code passing in the online editor but works as expected in the playground?
This works fine in the playground, not sure why it isn't working on Treehouse
/**
For this code challenge, let’s define a math operation as a function that
carries out some work on two integers and returns an integer as well. An
example is the function below, `differenceBetweenNumbers`, which takes two
integers and calculates the difference between the numbers. After calculating,
it returns the difference.
*/
func differenceBetweenNumbers(a: Int, b:Int) -> (Int) {
return a - b
}
// Enter your code below
func mathOperation(mathStuff: (Int, Int) -> Int, a: Int, b: Int) -> (Int) {
return mathStuff(a, b);
}
let difference = mathOperation(differenceBetweenNumbers, 5, 5)
3 Answers
Holger Liesegang
50,595 PointsHi Tony,
you might want to use func mathOperation(mathStuff: (Int, Int) -> Int, a: Int, b: Int) -> (Int) {
return mathStuff(a, b);
without the open and close brackets around Int
like so:
// Enter your code below
func mathOperation(mathStuff: (Int, Int) -> Int, a: Int, b: Int) -> Int {
return mathStuff(a, b);
}
let difference = mathOperation(differenceBetweenNumbers, 5, 5)
Chris Jones
Treehouse Guest TeacherTony, I'm in the same boat as you. I do want to note that I am using Xcode 7 Beta 3 locally.
func differenceBetweenNumbers(a: Int, b: Int) -> (Int) {
return a - b
}
func mathOperation(mathFunction: (Int, Int) -> Int, a: Int, b: Int) -> Int {
return mathFunction(a, b)
}
mathOperation(differenceBetweenNumbers, a: 4, b: 2)
This works just fine in a playground but will not pass in the code challenge. Maybe Pasan Premaratne has an answer?
Tony Luo
14,224 PointsHi Chris, I ended up making modifications that Holger suggested and I was able to pass the checks.
Try removing the labels a
and b
from your last line of code. I am using xcode 6.4 and it is showing an error in my playground.
Jonas Simkus
10,814 PointsIt worked for me when I removed "a" and "b" from mathOperation(differenceBetweenNumbers, a: 4, b: 2)
.
But then this line of code is incorrect, but only with incorrect line of code you can pass "THE Challenge".
mathOperation(differenceBetweenNumbers, 4, 2)
Unless, you do this for "mathOperation" function
func mathOperation(operation: (Int, Int) -> Int, _ a: Int, _ b: Int) -> Int {
return operation(a,b)
}
Tony Luo
14,224 PointsTony Luo
14,224 PointsHmm that ended up working. What is strange though is that the
differenceBetweenNumbers
function was able to pass the test no problem, even though the return type was wrapped in parens.Thanks Holger!