HTML Academy
Write, simplify
Functions13/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”
Make me beautiful
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Get rid of unnecessary

The number of bonus miles in MuffAir depends on the class and distance. To complete the program, you can add distance checks to each branch of the check for the flight class. But this option looks cumbersome.

The logic of calculating percentage is quite complicated, but I want to describe it as simply and elegantly as possible. Therefore, before adding a new condition, let’s change the structure of the existing code, but in such a way that it works as before. That is, we will refactor it.

Let’s look at the calculateMiles code. Inside, we have two ways out of the function:

var calculateMiles = function (distance, isBusinessClass) {
  if (isBusinessClass) {
    return …;
  } else {
    return …;
  }
};

It turns out that if we have an economy class, the program in any case will not get inside the first branch of the condition and will go further. Therefore, the else branch here is optional, it can be omitted.

var calculateMiles = function (distance, isBusinessClass) {
  if (isBusinessClass) {
    return …;
  }
  return …;
};
By the way,

Developers refactor their code to make it clear to colleagues and the author over time, to ensure that it can be easily maintained, that it did not contain repetitions, huge complex structures, and so on. Here, like in school: we first write a code in a draft, test it, scratch it, test it again, until we come up with a solution, and then neatly copy it into the final draft.

This refactoring is done by everyone, even by the coolest rock stars from the development world.

Comments

  • script.js
JavaScript
var calculateMiles = function (distance, isBusinessClass) { if (isBusinessClass) { return distance * 0.22; } else { return distance * 0.18; } // Move the code here }; 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 get ' + 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. Transfer the code from the else block down, after the condition operator.
  2. Delete else { } block.

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.