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 trialDaniel Schlichter
783 PointsWhats wrong with my code?
I have no idea what could be wrong with my code.. can anybody help :/ ?
// Enter your code below
func getRemainder (value a : Int,divisor b : Int) -> Int {
let modulo = a % b
return modulo }
let result = getRemainder(a: 10, b: 3)
1 Answer
Steve Hunter
57,712 PointsHi Daniel,
You probably need to use the external names you created for each parameter. Also, while it isn't incorrect and won't fail the challenge, you can omit the constant that you immediately return from the function - just return the result of the expression directly:
func getRemainder(value a: Int, divisor b: Int) -> Int{
return a % b
}
let result = getRemainder(value: 10, divisor: 3)
I hope that helps,
Steve.
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsIf you click the 'Preview' button with your code, it'll point you in the direction of your error like this:
Daniel Schlichter
783 PointsDaniel Schlichter
783 Pointsthanks it works but its a bit strange because the same code with the exactly same function doesn't works at the playground
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsThe Playground has no idea what you are asked to achieve in the challenge. The tests behind the challenge compiler are specifically looking for adherence to the question as set; it is expecting you to use the argument labels. The Playground doesn't know that. The Playground will run code that is syntactically correct but which will still fail the challenge.
By way of example, this "passes" the challenge by returning the result expected for the challenge example but is clearly wrong despite
result
holding the correct value that the challenge is expecting. This works fine in the Playground as it is syntactically correct but, clearly, should not pass for Treehouse's purposes.Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsThat said, in my Playground I get the same error.