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 mileage scheme with MuffAir. The company has a clever system for accruing miles. Business-class passengers receive 22% of the entire distance, and economy-class passengers only 18%. Also, if the distance exceeds 3500 km, the rate increases by another 15 percentage points.
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 a value that will indicate 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. Business class earns more miles, so we need to identify it and apply the higher rate. 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