Advertisement
Todorov_Stanimir

01. Animal Sanctuary

Jul 28th, 2019
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function animalSanctuary(input) {
  2.     const countAnimals = input.shift();
  3.     let totalWeight = 0;
  4.  
  5.     for (let i = 1; i <= countAnimals; i++) {
  6.         if ((line = (/n:(?<name>[^;]+);t:(?<kind>[^;]+);c--(?<country>[A-Za-z ]+)/g).exec(input.shift()))) {
  7.             totalWeight += calculateWeight(line.groups['name'].concat(line.groups['kind']));
  8.             console.log(`${extractNameAndKindAnimal(line.groups['name'])} is a ${extractNameAndKindAnimal(line.groups['kind'])} from ${line.groups['country']}`);
  9.         }
  10.     }
  11.     console.log(`Total weight of animals: ${totalWeight}KG`);
  12.  
  13.     function extractNameAndKindAnimal(string) {
  14.         return string
  15.             .split('')
  16.             .filter(char => {
  17.                 if (('a' <= char && char <= 'z') || ('A' <= char && char <= 'Z') || char === ' ')
  18.                     return true
  19.             })
  20.             .join('')
  21.     }
  22.  
  23.     function calculateWeight(string) {
  24.         return string.split('')
  25.             .map(Number)
  26.             .filter(char => char)
  27.             .reduce((a, b) => a + b, 0)
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement