Your First iOS and SwiftUI App · Challenge: Calculating the Difference | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4919757-your-first-ios-and-swiftui-app/lessons/26

I would imagine that most people can get within 10 of the target, so I have gone with squaring the difference (this removes the absolute number necessity, but does of course add one extra requirement of setting the score to zero if the difference is over 100 :slight_smile:

@mrpaulb Thank you for sharing your solution - much appreciated! :]

Instead of calculating difference *= -1 you can also use the method “negate()” just like difference.negate() that will do the same thing.
But there is a function aviable which makes the complete if statement redundant: “abs(_:)”
Just need one line of code: var difference: Int = abs(self.target - roundedValue)

@buessingjunge Thank you for sharing your solution - much appreciated! :]