Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(arr) {
- var name,
- type,
- taskNumber,
- score,
- lines;
- var result = {};
- for (var i = 0; i < arr.length; i++) {
- var obj = arr[i];
- var split = obj.split('&');
- name = split[0].trim();
- type = split[1].trim();
- taskNumber = split[2].trim();
- score = Number(split[3].trim());
- lines = Number(split[4].trim());
- taskNumber = 'Task ' + taskNumber;
- if (!(taskNumber in result)) {
- result[taskNumber] = {'tasks': [], 'average': 0, 'lines': 0};
- }
- result[taskNumber]['tasks'].push({'name': name, 'type': type});
- result[taskNumber]['average'] += score;
- result[taskNumber]['lines'] += lines;
- }
- for (var obj1 in result) {
- var avg = result[obj1].average / result[obj1].tasks.length;
- result[obj1].average = Math.round(avg * 100) / 100;
- result[obj1].tasks.sort(function (x, y) {
- return x.name.localeCompare(y.name);
- });
- }
- var a = [];
- var i = 0;
- for (var obj2 in result) {
- a.push({});
- a[i][obj2] = result[obj2];
- i++;
- }
- a.sort(function (x, y) {
- var xKey = Object.keys(x)[0];
- var yKey = Object.keys(y)[0];
- if (x[xKey].average !== y[yKey].average) {
- return y[yKey].average - x[xKey].average;
- }
- return x[xKey].lines - y[yKey].lines;
- })
- var json = JSON.stringify(a);
- json = json.substring(1, json.length-1).replace(/}},{/g, '},');
- console.log(json);
- }
- var arr = ['Array Matcher & strings & 4 & 100 & 38',
- 'Magic Wand & draw & 3 & 100 & 15',
- 'Dream Item & loops & 2 & 88 & 80',
- 'Knight Path & bits & 5 & 100 & 65',
- 'Basket Battle & conditionals & 2 & 100 & 120',
- 'Torrent Pirate & calculations & 1 & 100 & 20',
- 'Encrypted Matrix & nested loops & 4 & 90 & 52',
- 'Game of bits & bits & 5 & 100 & 18',
- 'Fit box in box & conditionals & 1 & 100 & 95',
- 'Disk & draw & 3 & 90 & 15',
- 'Poker Straight & nested loops & 4 & 40 & 57',
- 'Friend Bits & bits & 5 & 100 & 81'];
- solve(arr);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement