- script.js
JavaScript
var calculateMiles = function (distance, isBusinessClass) {
  var percent = 0.18;
  if (isBusinessClass) {
    percent += 0.04;
  }
  if (distance > 3500) {
    percent += 0.15;
  }
  return distance * percent;
};
var calculateFlights = function (distance, isBusinessClass, milesTarget) {
  var miles = calculateMiles(distance, isBusinessClass);
  var flights = Math.ceil(milesTarget / miles);
  return flights;
};
// Add an array and loop here
var flightsVariantFirst = calculateFlights(3118, true, 15000);
var flightsVariantSecond = calculateFlights(3617, false, 15000);
console.log('Required number of flights in business class to Valencia: ' + flightsVariantFirst);
console.log('Required number of economy flights 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);
}
Result
Goalscompleted
- After calculateFlightsfunction, create variabletargets, which contains array[3000, 7500, 15000].
- After variable targets, createforloop, which goes through arraytargetsfrom the very first element to the last one usingicounter.
- Move into the loop the entire code that is written below this loop.
- In calls of calculateFlightsfunction, replace the last argument with the current element oftargetsarray.
Comments