Advertisement
ggeorgiev88

army

Mar 14th, 2023
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function armies(data) {
  2.  
  3.     let armyCollect = {};
  4.  
  5.  
  6.     while (data.length > 1) {
  7.         let line = data.shift();
  8.         if (line.includes("arrives")) {
  9.             let leaderName = line.split(" arrives")[0];
  10.             armyCollect[leaderName] = [];
  11.         }
  12.         else if (line.includes(": ")) {
  13.             line = line.split(": ")
  14.             let leaderName = line.shift();
  15.             line = line[0].split(", ");
  16.             let armyName = line[0];
  17.             let armyCount = Number(line[1]);
  18.             if (armyCollect.hasOwnProperty(leaderName)) {
  19.                 armyCollect[leaderName].push(armyName, armyCount)
  20.             }
  21.         }
  22.         else if (line.includes(" + ")) {
  23.             line = line.split(" + ");
  24.             let armyName = line[0];
  25.             let armyToAdd = Number(line[1]);
  26.             let armyCollectEntries = Object.entries(armyCollect)
  27.  
  28.             for (let x = 0; x < armyCollectEntries.length; x++) {
  29.                 let currLeaderName = armyCollectEntries[x][0];
  30.                 let [currArmyName, currArmyCount] = armyCollectEntries[x][1];
  31.                 if (armyName === currArmyName) {
  32.                     currArmyCount += armyToAdd;
  33.                     armyCollect[currLeaderName].splice(1,1,currArmyCount)
  34.                 }
  35.  
  36.  
  37.  
  38.  
  39.             }
  40.  
  41.  
  42.  
  43.  
  44.         };
  45.  
  46.  
  47.     }
  48.  
  49.  
  50.  
  51.     console.table(armyCollect);
  52.    
  53.  
  54. }
  55.  
  56. armies(['Rick Burr arrives',
  57.     'Fergus: Wexamp, 30245',
  58.     'Rick Burr: Juard, 50000',
  59.     'Findlay arrives',
  60.     'Findlay: Britox, 34540',
  61.     'Wexamp + 6000',
  62.     'Juard + 1350',
  63.     'Britox + 4500',
  64.     'Porter arrives',
  65.     'Porter: Legion, 55000',
  66.     'Legion + 302',
  67.     'Rick Burr defeated',
  68.     'Porter: Retix, 3205']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement