Advertisement
Guest User

Untitled

a guest
Oct 15th, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr) {
  2.     let data = {};
  3.  
  4.     for (let i = 0; i < arr.length; i++) {
  5.         let row = arr[i];
  6.         row= row.split('|').filter(x=>x!='');
  7.  
  8.         let color = row[0];
  9.         let secondArg =row[1];
  10.         let thirdArg = row[2];
  11.  
  12.         if (!data.hasOwnProperty(color)) {
  13.             data[color] = {age: null, name: null, opponents:[], rank:0, win:0, loss:0 };
  14.         }
  15.  
  16.         if (secondArg=='age') {
  17.             data[color][secondArg] = (thirdArg);
  18.         } else if (secondArg=='name') {
  19.             data[color][secondArg]=thirdArg;
  20.         } else if(secondArg=='win') {
  21.             data[color][secondArg] +=1;
  22.             data[color].opponents.push(thirdArg);
  23.         } else if (secondArg=='loss') {
  24.             data[color][secondArg] +=1;
  25.             data[color].opponents.push(thirdArg);
  26.         }
  27.  
  28.         //ranks stays 0 until we collect all data
  29.     }
  30.  
  31.     //take all keys from the object which have no null values for name and age
  32.     let keys = Object.keys(data).filter(x=>data[x].name!=null && data[x].age!=null);
  33.  
  34.  
  35.     //create new object and pass valid data to it
  36.     let resultingData = {};
  37.     keys.forEach(x=> resultingData[x] = data[x]);
  38.     Object.keys(resultingData).forEach(x=>{
  39.         resultingData[x].rank=((resultingData[x].win+1)/(resultingData[x].loss+1)).toFixed(2);
  40.         //delete unnecessary properties
  41.         delete resultingData[x].win;
  42.         delete resultingData[x].loss;
  43.         //sort the array of opponents alphabetically
  44.         resultingData[x].opponents.sort((a,b)=> a.localeCompare(b));
  45.     });
  46.    
  47.     console.log(JSON.stringify(resultingData));
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement