Summary of “Functions”. Part 2

// Mile calculation function

var calculateMiles = function (distance, isBusinessClass) {
  var percent = 0.18;
  if (isBusinessClass) {
    percent += 0.04;
  }
  if (distance > 3500) {
    percent += 0.15;
  }
  return distance * percent;
};

// Function that calculates a number of flights

var calculateFlights = function (distance, isBusinessClass, milesTarget) {
  // Calling one function from the other
  var miles = calculateMiles(distance, isBusinessClass);
  var flights = Math.ceil(milesTarget / miles);
  return flights;
};

// Array of miles that must be accrued

var targets = [1500, 3000, 5000, 7500, 10000, 15000];

// Loop that figures out which flights will help us save the miles faster

for (var i = 0; i < targets.length; i++) {
  var flightsVariantFirst = calculateFlights(3118, true, targets[i]);
  var flightsVariantSecond = calculateFlights(3617, false, targets[i]);

  console.log('Required number of flights in business class to Valencia: ' + flightsVariantFirst);
  console.log('Required number of flights in economy class to Lisbon: ' + flightsVariantSecond);

  if (flightsVariantFirst > flightsVariantSecond) {
    console.log('You will save quicker flying economy to Lisbon! Number of flights: ' + flightsVariantSecond);
  } else {
    console.log('You will save quicker flying business class to Valencia! Number of flights: ' + flightsVariantFirst);
  }
}

Continue