By Димитър Петров


2020-08-01 13:38:44 8 Comments

function solve(input) {
  let avioCompanies = Number(input.shift())
  for (let i = 1; i <= avioCompanies; i++) {
    var command = input.shift()
    let passangers = Number(input.shift())
    let avg = 0
    let count2 = 0
    while (passangers !== "Finish") {
      avg += passangers
      count2++
      passangers = input.shift()
    }
    avg /= count2
    console.log(`${command}: ${Math.floor(avg)} passengers.`);


  }
  console.log(``);
}
solve([2, "Qatar Airways", 95, 231, 323, "Finish", "LOT", 76, 89, 55, "Finish"])

1 comments

@DavidXYZ 2020-08-01 13:47:54

You just need to keep track of the max while you are calculating the averages:

function findMaxAverage(input) {
    let numCompanies = Number(input.shift());
    let maxAvg = 0; maxCompany = "";
    for (let i = 0; i < numCompanies; i++) {
        let company = input.shift();
        let passengers = Number(input.shift());
        let total = 0;
        let count = 0;
        while (passengers !== "Finish") {
            total += passengers;
            count++;
            passengers = input.shift();
        }
        let avg = total / count;
        console.log(`${company}: ${Math.floor(avg)} passengers.`);
        if (avg > maxAvg) {
            maxAvg = avg;
            maxCompany = company;
        }

    }
    console.log(`----------`);
    console.log(`Maximum: ${maxCompany} with an average of ${Math.floor(maxAvg)} passengers.`);
}

findMaxAverage([2, "Qatar Airways", 95, 231, 323, "Finish", "LOT", 76, 89, 55, "Finish"]);

Related Questions

Sponsored Content

97 Answered Questions

[SOLVED] How can I remove a specific item from an array?

  • 2011-04-23 22:17:18
  • Walker
  • 6974239 View
  • 8528 Score
  • 97 Answer
  • Tags:   javascript arrays

49 Answered Questions

[SOLVED] How to check if an object is an array?

46 Answered Questions

[SOLVED] How can I check for an empty/undefined/null string in JavaScript?

83 Answered Questions

[SOLVED] How can I convert a string to boolean in JavaScript?

  • 2008-11-05 00:13:08
  • Kevin
  • 2091374 View
  • 2630 Score
  • 83 Answer
  • Tags:   javascript

30 Answered Questions

[SOLVED] How to append something to an array?

18 Answered Questions

[SOLVED] How to insert an item into an array at a specific index (JavaScript)?

11 Answered Questions

[SOLVED] How can I add new array elements at the beginning of an array in Javascript?

  • 2011-11-10 00:35:22
  • Moon
  • 837135 View
  • 1626 Score
  • 11 Answer
  • Tags:   javascript arrays

53 Answered Questions

32 Answered Questions

[SOLVED] How can I change an element's class with JavaScript?

  • 2008-10-12 20:06:43
  • Nathan Smith
  • 2628544 View
  • 2818 Score
  • 32 Answer
  • Tags:   javascript html dom

34 Answered Questions

[SOLVED] How can I upload files asynchronously?

Sponsored Content