Your First Flutter App: Polishing the App

Apr 12 2022 Dart 2.14.1, Flutter 2.5, Visual Studio Code

Part 1: Introduction to Dart

4. Challenge: Calculate the Difference

Lesson Complete

Play Next Lesson
Next
Save for later
About this episode
See forum comments
Cinema mode Mark as Complete Download course materials
Previous episode: 3. Write Methods Next episode: 5. Use If Statements

This video was last updated on Apr 12 2022

At this point, you have made a lot of progress on Bull’s Eye, and your programming to-do list is getting shorter and shorter.

Your next step is to calculate the player’s score, 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 would solve this problem to calculate the difference. Don’t worry about how to code it in Dart right now, just think about how you would do this in plain English.

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

How’d do you do with this challenge? 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 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’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.