HTML Academy
Business trip
Functions12/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”
Get rid of unnecessary
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Write, simplify

Remember, we talked about the fact that a function can return its result and placed a function call directly into an expression? Like that:

console.log('We’ll get ' + calculateMiles(4125) + ' miles for the flight');

And now let’s write the code more competently.

Once a function returns a value that can be used somewhere else (place into a line, add to a mathematical operation), why not write the result of this function into a variable and then use it? This way the code will be easier to read and edit.

Imagine a large program with hundreds of lines of code. You use the function call several times inside some expressions and the program works. And then you understand that you need to call the same function with other arguments, and you or your colleague have to look across the entire code for these function calls and fix them. And if you miss something, the program will not work correctly.

Comments

  • script.js
JavaScript
var calculateMiles = function (distance, isBusinessClass) { if (isBusinessClass) { return distance * 0.22; } else { return distance * 0.18; } };

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 defining the function, create variable milesEconom, which is equal to the result of the expression calculateMiles(3000, false);.
  2. Create another variable milesBusiness, which is equal to the result of calculateMiles(3000, true);.
  3. Log the following message in the console'With economy class from MuffAir, you’ll get ' + milesEconom + ' miles'.
  4. And then log this message in the console 'With business class of MuffAir, you’ll get ' + milesBusiness + ' 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.