HTML Academy
Going the second circle
Functions4/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”
Function, I’m calling you!
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Helper function

Have you already noticed the problem?

The program works, miles are calculated, but what happens if Muffin goes all out and decides to fly to a dozen cities more? We will constantly have to copy one part of the code, change the distance, calculate and declare the same variables over and over every time, which violates the rules of the language. Of course, we will cope with the task at hand, but we will spend a lot of time and the code will grow to include hundreds of lines.

Is there a more convenient and competent way to do calculations without copying? There is! Write a function.

A function is a piece of code that can be written once and then reused. A function, unlike a variable, does not simply contain a value, but performs a certain action and solves a certain task: it calculates, compares, searches. For example, our function will calculate miles accrued for the flight.

But first, let’s see what the functions consist of and how they are declared (created) in the code.

var functionName = function () {
  // Actions that are performed
  // by a function or a body
  // of the function
};

functionName is a name of the variable that contains the function. We can access the function using this name and we can use it many times. Let’s assume that the name of a variable is the name of the function.

There are other ways of creating functions, but we’ll discuss them later, in other courses.

Creating function is not more difficult than creating conditions or loops. Use the keyword function, then add parentheses. The curly brackets contain the actions that the function will perform every time we call it. Remember how we did it in the loops? There the code inside { } is called the body of the loop, and here the body of the function.

Write the function calculateMiles for calculating miles.

By the way, note the name calculateMiles. The function literally calculates miles, that is, does something. It’s a common naming convention: the function name contains a verb. This way it is more convenient for developers to navigate their own and even someone else’s code. Maybe the function was written by your colleague and you have no idea what’s inside. It will be enough for you to look at the function name in order to roughly imagine what it is intended for.

Comments

  • script.js
JavaScript
var percent = 0.25; var distance = 4125; if (distance > 10500) { percent = 0.35; } var miles = distance * percent; console.log('We’ll get ' + miles + ' miles for the flight to Irkutsk'); // Delete the code below var percent = 0.25; var distance = 11000; if (distance > 10500) { percent = 0.35; } var miles = distance * percent; console.log('We’ll get ' + miles + ' miles for the flight to Kamchatka');

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. Delete the entire code for calculating the miles of the second flight (variables, condition and log in the console).
  2. Add function declaration for counting miles at the end of the program: var calculateMiles = function () { };.
  3. Transfer the entire remaining code calculating miles into the body of the calculateMiles function.

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.