Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(arr) {
- let linesNumber = Number(arr.shift());
- let lines = arr;
- let pattern = /^n:(?<name>\w+[^;]+);t:(?<kind>[^;]+);c--(?<country>[A-Za-z ]+)$/g;
- let weightOfAnimals = 0;
- for (let i = 0; i < linesNumber; i++) {
- while ((validLine = pattern.exec(lines[i])) !== null) {
- let name = validLine.groups['name'].match(/[A-Za-z]+/g).join('');
- let kind = validLine.groups['kind'].match(/[A-Za-z]+/g).join('');
- let country = validLine.groups['country'];
- console.log(`${name} is a ${kind} from ${country}`);
- let weightOfAnimal = lines[i].match(/\d/g)
- .map(Number)
- .reduce((a, b) => a + b);
- weightOfAnimals += weightOfAnimal;
- }
- }
- console.log(`Total weight of animals: ${weightOfAnimals}KG`);
- }
- solve([ '4',
- 'n:Bo^%4b35454bie#$;t:Ele5ph#$34a%nt;c--Africa',
- 'n:Honey;t:Ti^^5ger;c--India',
- 'bla;t:1234a;c--America',
- 'n:A#$@545n;t:Cat241$@#23;cGermany' ]
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement