Your First iOS & SwiftUI App: An App from Scratch

Jan 11 2022 · Swift 5.5, iOS 15, Xcode 13

Part 3: Coding in Swift

24. Challenge: How to Calculate the Difference

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 23. Intro to Test-Driven Development Next episode: 25. If / Else Statements
Transcript: 24. Challenge: How to Calculate the Difference

At this point, you have created some Unit Tests to test the point calculation, and they currently fail because you haven’t written that code yet.

Your next step is to write that point calculation code, which is based on how close the player can get the slider to the target number.

And this leads me to your next challenge.

Your challenge is to pause the video, and write down in plain English how you would calculate the difference between the slider’s value, and the target value.

For example, if the target is 73, and the player drags the slider to 56, the player is within 17 of the target, so the difference should be 17.

Similarly, if the target is 73, and the player drags the slider to 85, the player is within 12 of the target, so the difference should be 12.

A simple approach would be to say the difference is the target value minus the slider’s value.

That works just fine in first case. However, it doesn’t work in the second case, because you would get a negative number.

So now it’s time for your challenge. Pause the video and write down in English how you whould solve this problem to calculate the difference. Don’t worry about how to code it in Swift right now, just think about how you would do this in plain English.

Allright that’s it - now pause the video, and good luck.

I came up with something like this:

  • If the slider’s value is greater than the target value, then the difference is: slider value minus the target value.

  • However, if the target value is greater than the slider value, then the difference is: target value minus the slider value.

  • Otherwise, both values must be equal, and the difference is zero.

This will always lead to a difference that is a positive number, because you always subtract the smaller number from the larger one.

Note that this is one way to calculate the difference, but there are other ways too. In fact, in a few episodes I’ll show you another way to calculate the difference that requires fewer lines of code. But for now, we’ll start with this approach.

You’ve just developed an algorithm, which is a fancy way of saying a series of steps for solving a computational problem. This is only a very simple algorithm, but it is one nonetheless.

There are many famous algorithms, such as quicksort for sorting a list of items and binary search for quickly searching through such a sorted list. Other people have already invented many algorithms that you can use in your own programs - that’ll save you a lot of thinking!

However, in the programs that you write, you’ll probably have to come up with a few algorithms of your own at some time or other. Some are simple like the one we just did; others can be pretty hard and might cause you to throw up your hands in despair. But that’s part of the fun of programming :]

The academic field of Computer Science concerns itself largely with studying algorithms and finding better ones.

Just like we did here, you can describe any algorithm in plain English. It’s just a series of steps that you perform to calculate something.

The point I’m trying to make is this: if you ever get stuck and you don’t know how to make your program calculate something, take a piece of paper and try to write out the steps in English.

Once you know how to do that, converting the algorithm to code should be a piece of cake.