Advertisement
Guest User

1.AnimalSanctuary- RetakeFinalExam18April2018

a guest
Jun 17th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr) {
  2.     let linesNumber = Number(arr.shift());
  3.     let lines = arr;
  4.     let pattern = /^n:(?<name>\w+[^;]+);t:(?<kind>[^;]+);c--(?<country>[A-Za-z ]+)$/g;
  5.     let weightOfAnimals = 0;
  6.  
  7.     for (let i = 0; i < linesNumber; i++) {
  8.         while ((validLine = pattern.exec(lines[i])) !== null) {
  9.             let name = validLine.groups['name'].match(/[A-Za-z]+/g).join('');
  10.             let kind = validLine.groups['kind'].match(/[A-Za-z]+/g).join('');
  11.             let country = validLine.groups['country'];
  12.             console.log(`${name} is a ${kind} from ${country}`);
  13.             let weightOfAnimal = lines[i].match(/\d/g)
  14.                 .map(Number)
  15.                 .reduce((a, b) => a + b);
  16.             weightOfAnimals += weightOfAnimal;
  17.         }
  18.     }
  19.     console.log(`Total weight of animals: ${weightOfAnimals}KG`);
  20. }
  21. solve([ '4',
  22.     'n:Bo^%4b35454bie#$;t:Ele5ph#$34a%nt;c--Africa',
  23.     'n:Honey;t:Ti^^5ger;c--India',
  24.     'bla;t:1234a;c--America',
  25.     'n:A#$@545n;t:Cat241$@#23;cGermany' ]
  26.  
  27. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement