HTML Academy
Get rid of unnecessary
Functions14/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”
Just add percentage
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Make me beautiful

The first step of refactoring is complete. The most important thing is that the function works the same as before the refactoring. It means you can move on. Look, the function duplicates the code for calculating miles. Expressions differ only in the value of percentages:

var calculateMiles = function (distance, isBusinessClass) {
  if (isBusinessClass) {
    return distance * 0.22;
  }
  return distance * 0.18;
};

Let’s take a look at the solution from a different angle: what if we calculated percentage in the conditions, rather than duplicating mile calculation? We can reformulate the task so that the base percentage is 18, and if we fly business class, the percentage is increased by 4. Then at the end of the function, we calculate and return miles once.

var calculateMiles = function (distance, isBusinessClass) {
  // Enter percentage
  if (isBusinessClass) {
    // Change percentage
  }
  // Calculate and return miles
};

The second step of refactoring is as follows: create variable percent, which will vary depending on the condition, and return the result of the expression distance * percent from the function.

This way the code is easier to understand, and there will be only one exit point from the function. The function will become easier to read, because the conditions for changing percentages can now be made not nested, but sequential.

By the way, the exit point from the function is the place where the function completes its work and logs the result.

Comments

  • script.js
JavaScript
var calculateMiles = function (distance, isBusinessClass) { if (isBusinessClass) { return distance * 0.22; } return distance * 0.18; }; var milesEconom = calculateMiles(3000, false); var milesBusiness = calculateMiles(3000, true); console.log('With economy class from MuffAir, you’ll get ' + milesEconom + ' miles'); console.log('With business class of MuffAir, you’ll save ' + milesBusiness + ' miles');

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. In the body of the function, it the very beginning, create variable percent equal to 0.18 on the 2nd line.
  2. Inside the condition, instead of calculating miles, increase variable percent by 0.04.
  3. Replace the expression that the function returns with distance * percent.

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.