HTML Academy
Seventh program: “From salary to salary”
Functions11/24
Back to the list of tasks
  • 1. Fasten your seat belts
  • 2. Long-distance flight
  • 3. Going the second circle
  • 4. Helper function
  • 5. Function, I’m calling you!
  • 6. I have a parameter for you
  • 7. Order of parameters
  • 8. Return from function
  • 9. Summary of “Functions”. Part 1
  • 10. Seventh program: “From salary to salary”
  • 11. Business trip
  • 12. Write, simplify
  • 13. Get rid of unnecessary
  • 14. Make me beautiful
  • 15. Just add percentage
  • 16. Call me quietly by name
  • 17. I see the target
  • 18. How many flights?
  • 19. Let’s clean up a little
  • 20. Who is faster?
  • 21. Saving up for a trip around the world
  • 22. Summary of “Functions”. Part 2
  • 23. Eighth program: “Money makes money”
  • 24. Ninth program: “The Eternal Question”
Write, simplify
  • Sign up
  • Log in

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.

Comments

  • script.js
JavaScript

What didn’t you like in this task?

Thanks! We’ll fix everything at once!

Console

The code has changed, click “Run” or turn autorun on.

Result

Goalscompleted
  1. Declare calculateMiles function with two parameters: distance and isBusinessClass.
  2. Add a flight class check. If we fly business class, the function returns distance * 0.22 miles.
  3. If we fly with economy, then we will return distance * 0.18 miles.

Cookies ∙ Privacy ∙ License Agreement ∙ About ∙ Contacts ∙ © HTML Academy OÜ, 2019−2025

VISAMastercard

Log in

or

Forgot your password?

Sign up

Sign up

or
Log in

Restore access

Have you forgotten your password or lost access to your profile? Enter your email connected to your profile and we will send you a link to restore access.

Forgot to connect your email to the profile? Email us and we’ll help.