HTML Academy
Make me beautiful
Functions15/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”
Call me quietly by name
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Just add percentage

The second step of refactoring is now behind us. The console has the same results: 660 miles and 540 miles. Which means that we did not ruin the logic and the refactoring was successful.

Due to the fact that we refactored our code, writing another if into the function is quite simple:

var calculateMiles = function (distance, isBusinessClass) {
  var percent = 0.18;
  if (isBusinessClass) {
    percent += 0.04;
  }
  if (distance > 3500) {
    // Let’s change percentage one more time
  }
  return distance * percent;
};

And what happens if we fail to enter a percentage variable and decrease the number of exits from the function?

calculateMiles code will look very scary. It will be something like this:

var calculateMiles = function (distance, isBusinessClass) {
  if (isBusinessClass && distance > 3500) {
    return distance * 0.37;
  }
  if (isBusinessClass && distance <= 3500) {
    return distance * 0.22;
  }
  if (!isBusinessClass && distance > 3500) {
    return distance * 0.33;
  }
  return distance * 0.18;
};

Or no less scary. Like this:

var calculateMiles = function (distance, isBusinessClass) {
  if (isBusinessClass) {
    if (distance > 3500) {
      return distance * 0.37;
    } else {
      return distance * 0.22;
    }
  } else {
    if (distance > 3500) {
      return distance * 0.33;
    } else {
      return distance * 0.18;
    }
  }
};

And this is only for two conditions that affect the mile calculation process. Imagine how cumbersome the code will become if there is one or two new conditions for calculating miles (which in real practice is quite common).

Comments

  • script.js
JavaScript
var calculateMiles = function (distance, isBusinessClass) { var percent = 0.18; if (isBusinessClass) { percent += 0.04; } return distance * percent; }; 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 will 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 calculateMiles function after checking the flight class, add another check. If the distance is greater than 3500 km, the percentage increases by another 0.15.
  2. Increase the distance in both function calls to 10000 to make sure that the number of miles has increased.

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.