HTML Academy
Who is faster?
Functions21/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”
Summary of “Functions”. Part 2
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Saving up for a trip around the world

We found out that miles to Tokyo are easier to save flying economy class to Lisbon. And what about other cities? Muffin warned us that Tokyo is only part of his plan. In addition to Tokyo, Muffin wants to fly for free to Saskatoon (3000 miles) and Asuncion (7500 miles).

Of course, we will not call function calculateFlights alone for each distance. We will write all the miles into an array (do not forget to add Tokyo there) and we’ll go through it, calling the calculateFlights function at each iteration for flights to Valencia and Lisbon. We will transfer to the function the current element of the array: the required number of miles for a flight to a city from Muffin’s list.

Why is it convenient to use an array and a loop? We can scale the solution to any number of cities, be it 2, 10 or even 100. The loop will go through the array, call a function to count the flights, and then compare the results. And we’ll repeat it for each element of the array.

Let’s add an array with a loop and let’s finally tell Muffin how he can save up for the trips of his dreams. Hurry up, Muffin is tired of waiting!

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; }; // Add an array and loop here var flightsVariantFirst = calculateFlights(3118, true, 15000); var flightsVariantSecond = calculateFlights(3617, false, 15000); console.log('Required number of flights in business class to Valencia: ' + flightsVariantFirst); console.log('Required number of economy flights to Lisbon: ' + flightsVariantSecond); if (flightsVariantFirst > flightsVariantSecond) { console.log('You will save quicker flying economy to Lisbon! Number of flights: ' + flightsVariantSecond); } else { console.log('You will save quicker flying business class to Valencia! Number of flights: ' + flightsVariantFirst); }

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 calculateFlights function, create variable targets, which contains array [3000, 7500, 15000].
  2. After variable targets, create for loop, which goes through array targets from the very first element to the last one using i counter.
  3. Move into the loop the entire code that is written below this loop.
  4. In calls of calculateFlights function, replace the last argument with the current element of targets array.

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.