HTML Academy
Just add percentage
Functions16/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”
I see the target
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Call me quietly by name

Meow!

You know, I’m a business cat, but I’m no stranger to simple human dreams. I really want to see the world, visit a lot of far-away cities, for example, Tokyo. They have everything I love there: fish, nature and technology. Let’s find out how I can quickly get it done?

There are a lot of business trips to two cities in my future: to Spanish Valencia and Portuguese Lisbon.

Flights to which city will help me save enough miles to fly to Tokyo quicker? I need 15,000 miles. I’ll fly business class to Valencia, but to get to Lisbon, I’ll have to settle for economy class.

Waiting for your reply! Talk to you soon!

Dreams must come true, especially if they’re the boss’s dreams!

We have a new task and a new program. If we think about it, the mile calculation function that we wrote earlier will be very useful for us here. First, we will use it to find out how many miles we can get for one flight, and then how many of such flights we need to complete in order to accrue the 15000 miles needed to fly to Tokyo. Muffin wants to fly to other cities, but first let’s solve the task using one example, and then easily apply the solution to other distances.

We will create another function that will count the number of flights, use it to call our first mile calculation function and then use the value obtained.

It sounds more complicated than it actually is. A good program always consists of many parts, each of which deals only with its task. As in our case: one function counts miles, and the other one counts flights. In this case, it is very convenient to use one function to solve the tasks of the other one. For example, to calculate the number of flights, we will need to first find out the number of miles that Muffin will receive per flight with this destination. And how do we do it? Call the calculateMiles function, write the result to a variable and use it in further calculations in the flight count function.

Let’s practice what we learned!

Create calculateFlights function. Let’s count the flights for the first option, the flight to Valencia: the distance to Valencia is 3118 km. We will write the function in several steps; at first we will just test the call of one function from the other.

Comments

  • script.js
JavaScript
var calculateMiles = function (distance, isBusinessClass) { var percent = 0.18; if (isBusinessClass) { percent += 0.04; } if (distance > 3500) { percent += 0.15; } return distance * percent; };

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. After the calculateMiles function, declare function calculateFlights with two parameters distance and isBusinessClass.
  2. Inside the new function, declare variable miles, which is equal to the result of function calculateMiles(distance, isBusinessClass).
  3. Then log in the console the value of variable 'Miles per flight: ' + miles.
  4. Below, after declaring all functions, call function calculateFlights(3118, true);.

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.