HTML Academy
How many flights?
Functions19/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”
Who is faster?
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Let’s clean up a little

Let’s refactor our code and then finish writing the program for Muffin.

Why refactor again?

The thing is that later, we will need to compare the obtained results and log recommendations for the boss. It turns out that we will have to use the results of calculateFlights several times. Therefore, it is logical and correct to write these results into variables to store them for use later on. If we suddenly want to get data for other cities, we just change the values ​​of the arguments in one single place, not across the entire code.

What is the best way to name variables that contain the number of flights to Lisbon and Valencia? The first thing that comes to mind is to give the variables specific names: flightsToValencia and flightsToLisbon, but these names are too specific. If Muffin wants to fly to, say, Canadian Winnipeg instead of Valencia, the variable will have to be renamed. And it will have to be done every time the destination of the flight changes.

Instead of the destination, you could use the flight class to name the variables: flightsEconom and flightsBusiness. This option is also not the best one, because when we fly to different cities the class changes, but also the distance changes.

Let’s name the variables as simply as possible: flightsVariant1 and flightsVariant2. Then inside they can have any distances and flight classes.

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; }; var calculateFlights = function (distance, isBusinessClass, milesTarget) { var miles = calculateMiles(distance, isBusinessClass); var flights = Math.ceil(milesTarget / miles); return flights; }; console.log('Required number of flights in business class to Valencia: ' + calculateFlights(3118, true, 15000)); console.log('Required number of economy flights to Lisbon: ' + calculateFlights(3617, false, 15000));

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. Below, after declaring the flight calculation function, write calculateFlights(3118, true, 15000) in the new variable flightsVariant1.
  2. When logging the message, replace calculateFlights(3118, true, 15000) with flightsVariant1.
  3. Write the result of calling the function calculateFlights(3617, false, 15000) in variable flightsVariant2.
  4. When logging the message, replace calculateFlights(3617, false, 15000) with flightsVariant2.

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.