0

I'm making a program that can return the highest 5 values from my 'average' function.

This is my code:

Person.prototype.average = function(){

    var plus=0;
    for (var i = 0; i < this.kilometers.length; i++) {
        plus += this.kilometers[i];
    }
    var total= plus/this.kilometers.length;
    return total;
};

function myFunction(runners) {
  var list = [];
  var plus = 0;
  for (var i = 0; i < runners.length; i++) {
    console.log(runners[i].average());
  }
}

var runners = [new Person('Alex', 'male', '25', [4, 3, 3, 3, 5, 2.9, 4.2]),
  new Person('Roberto', 'male', '70', [5, 4, 5, 3.2, 4.3, 5, 3]),
  new Person('Silverio', 'male', '45', [3, 3, 3, 3, 3, 3, 4]),
  new Person('Ramon', 'male', '50', [5, 5, 5, 5, 4, 3, 3]),
  new Person('Arturo', 'male', '24', [3, 3.4, 4, 5, 4.9, 2.9, 4, 3]),
  new Person('Sol', 'female', '25', [4, 3, 4, 3, 4, 3, 4]),
  new Person('Sarai', 'female', '20', [5, 5, 5, 5, 5, 5, 3]),
  new Person('Alexa', 'female', '22', [4, 3, 2, 4, 4, 5, 3.8]),
  new Person('Betty', 'female', '24', [3.1, 3.2, 2.9, 2, 3.3, 3.4, 2.8]),
  new Person('Daniela', 'female', '25', [5, 5, 4, 3, 4, 2.8, 5])
];

I know I have to use the sort method first but I'm not sure of how to do it.

Alex Dis
  • 25
  • 6
  • 4
    Possible duplicate of [Sort array of objects by string property value in JavaScript](https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value-in-javascript) – Isaac Apr 04 '18 at 23:33
  • 2
    Post your code as plain text, not an image. See https://stackoverflow.com/help/formatting for code formatting help. – Barmar Apr 04 '18 at 23:36
  • Use `sort` with a comparison function that compares the averages of the two parameters. It would probably be best if you cached the average in a property of the object, so you don't have to recalculate it. – Barmar Apr 04 '18 at 23:39
  • @Isaac He doesn't want to sort by a property. He wants to sort by a function that's calculated over the object. – Barmar Apr 04 '18 at 23:40
  • @Barmar it will be the same approach, `runners.sort( (l,r) => l.average() - r.average())` – Isaac Apr 04 '18 at 23:45
  • I've edited it! and yes that's correct I need to sort by the result pf a function, now I got a question, I'm sorry this code maybe it's too advanced for me but what does '=>' is doing? – Alex Dis Apr 04 '18 at 23:50
  • @AlexDis the `=>` notation is another way to make functions. The arguments are on the left , and the body is on the right. `var add = (a, b) => a+b` makes a function to add two things. – Mark Apr 04 '18 at 23:53

3 Answers3

0
Person.prototype.average = function() {
    var plus = 0;
    for(var i=0;i<this.kilometers.length;i++){
        plus += this.kilometers[i];
    }
    var total = plus/this.kilometers.length;
    return this.kilometers.slice().sort().slice(-5); // you have to slice first if you don't want to modify original array
}

Here is documentation of used methods:

Michał Z.
  • 1,322
  • 1
  • 10
  • 17
0

You can pass a function into array.sort() that calls you average() function when comparing two values. For example:

let averages = runners.sort((a,b) => a.average() - b.average())

Now averages will have a list of Persons sorted by average. You can then slice that list to get the top five.

averages.slice(0,5)

Here's a more complete example:

function Person(name, gender, age, scores){
    this.name = name;
    this.gender = gender;
    this.age  = age;
    this.scores = scores;
}
Person.prototype.average = function(){
    // shorter average function
    return this.scores.reduce((a,c) => a+c, 0)/this.scores.length
}

var runners = [new Person('Alex', 'male', '25', [4, 3, 3, 3, 5, 2.9, 4.2]),
  new Person('Roberto', 'male', '70', [5, 4, 5, 3.2, 4.3, 5, 3]),
  new Person('Silverio', 'male', '45', [3, 3, 3, 3, 3, 3, 4]),
  new Person('Ramon', 'male', '50', [5, 5, 5, 5, 4, 3, 3]),
  new Person('Arturo', 'male', '24', [3, 3.4, 4, 5, 4.9, 2.9, 4, 3]),
  new Person('Sol', 'female', '25', [4, 3, 4, 3, 4, 3, 4]),
  new Person('Sarai', 'female', '20', [5, 5, 5, 5, 5, 5, 3]),
  new Person('Alexa', 'female', '22', [4, 3, 2, 4, 4, 5, 3.8]),
  new Person('Betty', 'female', '24', [3.1, 3.2, 2.9, 2, 3.3, 3.4, 2.8]),
  new Person('Daniela', 'female', '25', [5, 5, 4, 3, 4, 2.8, 5])
];

let averages = runners.sort((a,b) => b.average() - a.average())

let topFive = averages.slice(0,5)
console.log(topFive.map(n => `name: ${n.name}: Average: ${n.average()}`))
Mark
  • 90,562
  • 7
  • 108
  • 148
0
function myFunction(a, b) {
    return a.average() > b.average() ? -1 : 1;
}

runners.sort(myFunction).splice(0, 5)

// for example
function Person(name) {
 this.name = name;
}

Person.prototype.average = function(){
    var plus=0;
    for (var i = 0; i < this.kilometers.length; i++) {
        plus += this.kilometers[i];
    }
    var total= plus/this.kilometers.length;
    return total;
};

var runners = [new Person('Alex', 'male', '25', [4, 3, 3, 3, 5, 2.9, 4.2]),
  new Person('Roberto', 'male', '70', [5, 4, 5, 3.2, 4.3, 5, 3]),
  new Person('Silverio', 'male', '45', [3, 3, 3, 3, 3, 3, 4]),
  new Person('Ramon', 'male', '50', [5, 5, 5, 5, 4, 3, 3]),
  new Person('Arturo', 'male', '24', [3, 3.4, 4, 5, 4.9, 2.9, 4, 3]),
  new Person('Sol', 'female', '25', [4, 3, 4, 3, 4, 3, 4]),
  new Person('Sarai', 'female', '20', [5, 5, 5, 5, 5, 5, 3]),
  new Person('Alexa', 'female', '22', [4, 3, 2, 4, 4, 5, 3.8]),
  new Person('Betty', 'female', '24', [3.1, 3.2, 2.9, 2, 3.3, 3.4, 2.8]),
  new Person('Daniela', 'female', '25', [5, 5, 4, 3, 4, 2.8, 5])
];

function myFunction(a, b) {
    return a.average > b.average ? -1 : 1;
}

console.log(runners.sort(myFunction).splice(0, 5));
Arrararr
  • 368
  • 2
  • 12