Your First iOS App: Coding in Swift

Jul 28 2026 · Swift 6.3, iOS 26, Xcode 26

Lesson 06: Challenge: Calculate the Difference

Challenge: Calculate the Difference

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

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.

func points(sliderValue: Int) -> Int {
  var difference: Int = target - sliderValue
  if difference < 0 {
    difference = difference * -1
    // or difference *= -1
    // or difference = -difference
  }
    difference = difference * -1
    // or difference *= -1
    difference = difference * -1
    // or difference *= -1
    // or difference = -difference
See forum comments
Cinema mode Download course materials from Github
Previous: If Else Statements Next: Variables & Constants