Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ranking the users based on percentage using map/filter/reduce

Tags:

javascript

I want to display all the details of the users based on their percentage of marks and assign ranks to them. Here I'm getting all the details. How to sort and print the users like.

Tom has scored marks in Science - 80 Maths - 89 English - 91 and secured 86.67 % and his rank is 1.  

Similarly every user in the students object.

var students = [{
  name: "John",
  age: 20,
  id: 1,
  marks: {
    science: 80,
    maths: 90,
    english: 67
  }
}, {
  name: "Jack",
  age: 22,
  id: 3,
  marks: {
    science: 56,
    maths: 91,
    english: 81
  }
}, {
  name: "Robert",
  age: 23,
  id: 2,
  marks: {
    science: 75,
    maths: 79,
    english: 87
  }
}, {
  name: "Tom",
  age: 20,
  id: 4,
  marks: {
    science: 80,
    maths: 89,
    english: 91
  }
}];


var inDetail = getPercentageAndRank(students);

function getPercentageAndRank(students) {

  var details = students.map(function(getMarks) {
    console.log(((getMarks.marks.english + getMarks.marks.maths + getMarks.marks.science) / 3));
    return {
      name: getMarks.name,
      science: getMarks.marks.science,
      maths: getMarks.marks.maths,
      english: getMarks.marks.english,
      percentage: ((getMarks.marks.english + getMarks.marks.maths + getMarks.marks.science) / 3)
    };
  })

  return details.map(function(gg) {
    return [
      gg.name,
      "has scored marks in Science -",
      gg.science,
      "Maths -",
      gg.maths,
      "English -",
      gg.english,
      "and secured", (gg.percentage).toFixed(2),
      "%."
    ].join(' ');

  });
}

Output:

["John has scored marks in Science - 80 Maths - 90 English - 67 and secured 79.00 %.",
    "Jack has scored marks in Science - 56 Maths - 91 English - 81 and secured 76.00 %.",
    "Robert has scored marks in Science - 75 Maths - 79 English - 87 and secured 80.33 %.",
    "Tom has scored marks in Science - 80 Maths - 89 English - 91 and secured 86.67 %."]

Here the output I'm getting is not according to the above said. How can I get as above..

like image 888
disciple Avatar asked Feb 18 '26 20:02

disciple


1 Answers

You need to add:

.sort(function(a, b) {
    return b.percentage - a.percentage;
  });

Check this solution.

var students = [{
  name: "John",
  age: 20,
  id: 1,
  marks: {
    science: 80,
    maths: 90,
    english: 67
  }
}, {
  name: "Jack",
  age: 22,
  id: 3,
  marks: {
    science: 56,
    maths: 91,
    english: 81
  }
}, {
  name: "Robert",
  age: 23,
  id: 2,
  marks: {
    science: 75,
    maths: 79,
    english: 87
  }
}, {
  name: "Tom",
  age: 20,
  id: 4,
  marks: {
    science: 80,
    maths: 89,
    english: 91
  }
}];


var inDetail = getPercentageAndRank(students);

function getPercentageAndRank(students) {

  var details = students.map(function(getMarks) {
    // console.log(((getMarks.marks.english + getMarks.marks.maths + getMarks.marks.science) / 3));
    return {
      name: getMarks.name,
      science: getMarks.marks.science,
      maths: getMarks.marks.maths,
      english: getMarks.marks.english,
      percentage: ((getMarks.marks.english + getMarks.marks.maths + getMarks.marks.science) / 3),
      rank: 0
    };
  }).sort(function(a, b) {
    return b.percentage - a.percentage;
  });

  for (var i = 0; i < details.length; i++) {
    // Updating the rank in order.
    details[i].rank = i + 1;
  }

  return details.map(function(gg) {
    return [
      gg.name,
      "has scored marks in Science -",
      gg.science,
      "Maths -",
      gg.maths,
      "English -",
      gg.english,
      "and secured", (gg.percentage).toFixed(2),
      "%", "and his rank is", gg.rank + "."
    ].join(' ');

  }).reduce(function(previous, current) {
    return previous + '\n' + current;
  }); // String result...
}
console.log(inDetail);

And in this question you don't need a filter function.

like image 60
Danny Fardy Jhonston Bermúdez Avatar answered Feb 20 '26 08:02

Danny Fardy Jhonston Bermúdez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!