Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

##Output

Output

##Output

Output

added 135 characters in body
Source Link
Jonah
  • 4.4k
  • 15
  • 23
function bestOfPairs(input) {
 var pairId = elm => elm.id.split('-').sort().join(''), // creates an id that's the same for both elements of a "pair"
 byWeight = (a, b) => parseInt(b.weight) - parseInt(a.weight); // reverse sort by weight: best candidate will be first in array
 grouped = input.reduce((m, x) =>
 (m[pairId(x)] = (m[pairId(x)] || []).concat(x)) && m // groups together elements as pairs, based on their id
 , {});
 return Object.keys(grouped).map(id => { // each "id" here is the "pairs" id, that is, the id being used to create the groups
 var sorted = grouped[id].sort(byWeight);,
 return invalid = sorted[1] == undefined || 
  sorted[0].weight == sorted[1].weightweight;
  return invalid ? null : sorted[0]; // returns the element with higher weight, or null if they're equal, or there's only one
 })
 .filter(x => x !== null); // this removes elments with the same weight
}
function bestOfPairs(input) {
 var pairId = elm => elm.id.split('-').sort().join(''), // creates an id that's the same for both elements of a "pair"
 byWeight = (a, b) => parseInt(b.weight) - parseInt(a.weight); // reverse sort by weight: best candidate will be first in array
 grouped = input.reduce((m, x) =>
 (m[pairId(x)] = (m[pairId(x)] || []).concat(x)) && m // groups together elements as pairs, based on their id
 , {});
 return Object.keys(grouped).map(id => { // each "id" here is the "pairs" id, that is, the id being used to create the groups
 var sorted = grouped[id].sort(byWeight);
 return sorted[0].weight == sorted[1].weight ? null : sorted[0]; // returns the element with higher weight, or null if they're equal
 })
 .filter(x => x !== null); // this removes elments with the same weight
}
function bestOfPairs(input) {
 var pairId = elm => elm.id.split('-').sort().join(''), // creates an id that's the same for both elements of a "pair"
 byWeight = (a, b) => parseInt(b.weight) - parseInt(a.weight); // reverse sort by weight: best candidate will be first in array
 grouped = input.reduce((m, x) =>
 (m[pairId(x)] = (m[pairId(x)] || []).concat(x)) && m // groups together elements as pairs, based on their id
 , {});
 return Object.keys(grouped).map(id => { // each "id" here is the "pairs" id, that is, the id being used to create the groups
 var sorted = grouped[id].sort(byWeight),
  invalid = sorted[1] == undefined || 
  sorted[0].weight == sorted[1].weight;
  return invalid ? null : sorted[0]; // returns the element with higher weight, or null if they're equal, or there's only one
 })
 .filter(x => x !== null); // this removes elments with the same weight
}
deleted 51 characters in body
Source Link
Jonah
  • 4.4k
  • 15
  • 23
function bestOfPairs(input) {
 var pairId = elm => elm.id.split('-').sort().join(''), // creates an id that's the same for both elements of a "pair"
 byWeight = (a, b) => parseInt(b.weight) - parseInt(a.weight; weight); // reverse sort by weight: best candidate will be first in array
 grouped = input.reduce((m, x) =>
 (m[pairId(x)] = (m[pairId(x)] || []).concat(x)) && m // groups together elements as pairs, based on their id
 , {});
 return Object.keys(grouped).map(id => { // each "id" here is the "pairs" id, that is, the id being used to create the groups
 var sorted = grouped[id].sort(byWeight);
 return sorted[0].weight == sorted[1].weight ? null : sorted[0]; // returns the element with higher weight, or null if they're equal
 })
 .filter(x => x !== null); // this removes elments with the same weight
}
[ { id: 'A-A', x: '0', y: '2', weight: '100' },
  { id: 'A-B', x: '1', y: '0', weight: '100' },
 { id: 'A-C', x: '2', y: '0', weight: '77' },
 { id: 'A-D', x: '3', y: '0', weight: '95' },
 { id: 'B-C', x: '1', y: '2', weight: '3' },
 { id: 'D-F', x: '5', y: '3', weight: '18' } ]
function bestOfPairs(input) {
 var pairId = elm => elm.id.split('-').sort().join(''), // creates an id that's the same for both elements of a "pair"
 byWeight = (a, b) => b.weight - a.weight;  // reverse sort by weight: best candidate will be first in array
 grouped = input.reduce((m, x) =>
 (m[pairId(x)] = (m[pairId(x)] || []).concat(x)) && m // groups together elements as pairs, based on their id
 , {});
 return Object.keys(grouped).map(id => { // each "id" here is the "pairs" id, that is, the id being used to create the groups
 var sorted = grouped[id].sort(byWeight);
 return sorted[0].weight == sorted[1].weight ? null : sorted[0]; // returns the element with higher weight, or null if they're equal
 })
 .filter(x => x !== null); // this removes elments with the same weight
}
[ { id: 'A-A', x: '0', y: '2', weight: '100' },
  { id: 'A-B', x: '1', y: '0', weight: '100' },
 { id: 'A-C', x: '2', y: '0', weight: '77' },
 { id: 'A-D', x: '3', y: '0', weight: '95' },
 { id: 'B-C', x: '1', y: '2', weight: '3' },
 { id: 'D-F', x: '5', y: '3', weight: '18' } ]
function bestOfPairs(input) {
 var pairId = elm => elm.id.split('-').sort().join(''), // creates an id that's the same for both elements of a "pair"
 byWeight = (a, b) => parseInt(b.weight) - parseInt(a.weight); // reverse sort by weight: best candidate will be first in array
 grouped = input.reduce((m, x) =>
 (m[pairId(x)] = (m[pairId(x)] || []).concat(x)) && m // groups together elements as pairs, based on their id
 , {});
 return Object.keys(grouped).map(id => { // each "id" here is the "pairs" id, that is, the id being used to create the groups
 var sorted = grouped[id].sort(byWeight);
 return sorted[0].weight == sorted[1].weight ? null : sorted[0]; // returns the element with higher weight, or null if they're equal
 })
 .filter(x => x !== null); // this removes elments with the same weight
}
[ { id: 'A-B', x: '1', y: '0', weight: '100' },
 { id: 'A-C', x: '2', y: '0', weight: '77' },
 { id: 'A-D', x: '3', y: '0', weight: '95' },
 { id: 'B-C', x: '1', y: '2', weight: '3' },
 { id: 'D-F', x: '5', y: '3', weight: '18' } ]
deleted 1 character in body
Source Link
Jonah
  • 4.4k
  • 15
  • 23
Loading
Source Link
Jonah
  • 4.4k
  • 15
  • 23
Loading
default

AltStyle によって変換されたページ (->オリジナル) /