This video Challenge: Calculate the Difference was last updated on Jul 5 2022
You’ve already made a lot progress on bullseye. And your programming TODO list is getting shorther.
The next step is to calculate the player’s score. This value is calculated based on how close the player’s selection is to the target value.
I want you to 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, well 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. Well that works just fine in the first case. However, it doesn’t work in the second case, because you would get a negative number.
Go ahead and pause the video and write down how you would solve this problem to calculate the difference. Dont think about the kotlin code you’re going to write for now.
Just think of the steps to solve the problem in plain English.
How did you do with this challenge? Here’s what I came up with:

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.
Do 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 just 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 quick sort 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 will save you a lot of time and thinking!
However, in the programs that you write, you’ll probably still have to come up with a few algorithms of your own at some point.
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 :]
Now 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 solve a problem.
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.
Then i eat a piece of cake $[\]