Your next career begins with
Save 50% off your seat in our next iOS Bootcamp. Limited time only. Sessions start April 3.
Your First iOS & SwiftUI App: An App from Scratch
Jan 11 2022 Swift 5.5, iOS 15, Xcode 13
Part 3: Coding in Swift
26. Challenge: Calculate the Difference

Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
About this episode
Try improving the algorithm to calculate the difference so it is written in fewer lines of code.
Instructors
Contributors
Instructor
Instructor
Illustrator
Video Editor
Over 300 content creators. Join our team.
Version history
iOS 15, Swift 5.5, Xcode 13 (Selected)
Jan 11 2022iOS 16, Swift 5.7, Xcode 14
Feb 13 2023iOS 14, Swift 5, Xcode 12
Dec 15 2020iOS 13, Swift 5, Xcode 11
Sep 3 2019iOS 12, Swift 4, Xcode 10
Jul 24 2018iOS 11, Swift 4, Xcode 9
Sep 19 2017iOS 10, Swift 3, Xcode 8
Nov 7 2016Swift 2
Sep 1 2015Leave a rating/review
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
This video Challenge: Calculate the Difference was last updated on Jan 11 2022
In the previous challenge, I mentioned that there’s another way to calculate the difference that requires fewer lines of code - and that’s the subject of this challenge.
The new algorithm looks like this:
- First, subtract the target value from the slider’s value.
- Then, if the result is a negative number, then multiply it by -1 to make it a positive number.
At this point, pause the video and try changing the difference calculation to this new algorithm. Then keep watching to compare your work to my solution.
Alright that’s it - good luck.
func points(sliderValue: Int) -> Int {
var difference: Int = self.target - sliderValue
if difference < 0 {
difference = difference * -1
// or difference *= -1
// or difference = -difference
}
var awardedPoints: Int = 100 - difference
return awardedPoints
}
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development — plans start at just $19.99/month! Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.
Learn more