Skip to content

Instantly share code, notes, and snippets.

@mhf-ir
Last active September 26, 2018 13:46
Show Gist options
  • Save mhf-ir/87444baa342ccd9a0c0bbdf63c311623 to your computer and use it in GitHub Desktop.
Save mhf-ir/87444baa342ccd9a0c0bbdf63c311623 to your computer and use it in GitHub Desktop.

Repository with testunit, code coverage and api generation:

https://github.com/mhf-ir/chasersample

Sample you need:

var samples = [
  {
    name: 'b',
    ranking: 1,
  },
  {
    name: 'a',
    ranking: 0,
  },
  {
    name: 'c',
    ranking: 2,
  },
];

// -- sort

var sortedArray = samples.slice(0); // for clone
sortedArray.sort(function(a, b) {
  return a.ranking - b.ranking;
});

console.log(sortedArray);

// -- average
var average = samples.map((a) => {
  return a.ranking;
}).reduce(
  (a, b) => {
    return a + b;
  },
  0,
) / samples.length;

console.log(average);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment