Loading…
Everything will be ready in few seconds
- Theory
- Theory
- Comments
Business trip
Muffin sent us a new task. The boss got hooked and now wants to accrue miles on business trips.
For business trips, Muffin uses a different carrier: MuffAir. The company has a clever system for accruing miles. Passengers of business class receive 22%
of the entire distance, and passengers of economy class only 18%
. Also, if the distance exceeds 3500
km, the percentage increases by another 15%
.
For the new task, we will create a new function and go over what we have already learned.
The name of the last function for calculating miles calculateMiles
accurately describes the purpose of this function and is suitable for the new task. Let’s leave it.
The new function will accept two input parameters: the distance and some sign that will specify the flight class for the program. With distance everything is simple, let’s call parameter distance
.
And what about the second parameter?
In our case, for the second parameter, only two options are sufficient: business class or not business class. Because percentage increases only for business class, it’s important for us not to miss this moment and accrue additional interest. In this case, a parameter that will only contain Boolean values will work (true
and false
). Let true
denote business class, and false
everything else.
All we have left to do is figure out the name of the second parameter. Similar values are called flags, and they are named in a special way. For example, in our case, name isBusinessClass
will work. It’s as if the variable asks the question to its content “Do we have business class now?”, and the content answers this question “yes” (true
) or “no” (false
).
Let’s start creating a function for the new task from the Boss.
- script.js
Thanks! We’ll fix everything at once!
The code has changed, click “Run” or turn autorun on.
Comments