Your First iOS & SwiftUI App: An App from Scratch

Feb 13 2023 · Swift 5.7, iOS 16, Xcode 14

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

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

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. This leads me to your next challenge. Your challenge is to pause the video and write down, just 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 sliders value and that works just fine in the 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 would solve this problem to calculate the difference. Don't worry about how to code it in Swift. Just think about how you would do this in plain English. All right, that's it. Pause the video and good luck. (bright lively music) 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 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 list of items and binary search for quickly searching through such assorted list. Other people have already invented many algorithms that you can use in your own projects 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 another. 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 to calculate something, take a piece of paper and try to write up the steps just in plain English. Once you know how to do that, converting the algorithm to code should be a piece of cake.