HTML Academy
Call me quietly by name
Functions17/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”
How many flights?
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

I see the target

Now that we know the number of miles per flight, nothing prevents us from knowing how many times we will have to fly to the same destination in order to get a trip to Tokyo.

Let’s add another parameter to our calculateFlights function. It will contain the number of miles required. Now it’s miles to Tokyo, but what if Muffin decides to save up for a flight somewhere else? In that case, all we would have to do is change the value of the argument and call the function.

Since we are making a universal program, the name of the new parameter also must be universal. Therefore a name like milesToTokyo is not for purpose; it is too specific and cannot be re-used. Choose milesTarget. This name fits better, because with such a parameter we can calculate the number of flights even to Seattle.

To get the number of flights, we need to divide milesTarget by miles accrued for one flight. Note that the result may not be an integer. Since we are dealing with flights, not taking into account the fractional part when rounding up is incorrect. For example, if we need 25.2 flights to get 15000 miles, this means that we should have 26 flights as a result. Otherwise we will be just a few miles short. Therefore, in our task, we will round the result of the calculations up, using Math.ceil().

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) { var miles = calculateMiles(distance, isBusinessClass); console.log('Miles for the flight: ' + miles); }; calculateFlights(3118, true);

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. Create third parameter milesTarget in calculateFlights function and transfer third argument 15000 in the call of calculateFlights function.
  2. Inside calculateFlights function, after logging miles in the console, declare variable flights, which equals milesTarget / miles.
  3. Log the number of flights in the console 'Number of flights:' + flights.
  4. Round the number of flights up Math.ceil(milesTarget / 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.