Advertisement
Todorov_Stanimir

01. Animal Sanctuary

Jun 17th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function animalSanctuary(input) {
  2.     let countAnimals = input[0];
  3.     let weight = 0;
  4.     for (let numberAnimal = 1; numberAnimal <= countAnimals; numberAnimal++) {
  5.         let pattern = /^n:{1}[^;]*;{1}t:{1}[^;]*;c--{1}[A-Za-z\s]+$/g;
  6.         if (input[numberAnimal].match(pattern)) {
  7.             let [animalName, animalKind, animalCountry] = input[numberAnimal].split(';');
  8.             animalName = animalName.split('n:')[1];
  9.             animalKind = animalKind.split('t:')[1];
  10.             animalCountry = animalCountry.split('c--')[1];
  11.  
  12.             [name, weightName] = checkNameAndKind(animalName);
  13.             [kind, weightKind] = checkNameAndKind(animalKind);
  14.             weight += weightName + weightKind;
  15.             country = checkCountry(animalCountry);
  16.             console.log(`${name} is a ${kind} from ${country}`);
  17.         }
  18.     }
  19.     console.log(`Total weight of animals: ${weight}KG`);
  20.  
  21.     function checkNameAndKind(arg1) {
  22.         let output = '';
  23.         let weight = 0
  24.         arg1 = arg1.split('');
  25.         let pattern1 = /[a-z\s]/gi;
  26.         let pattern2 = /[0-9]/g;
  27.         arg1.forEach(el => {
  28.             if (el.match(pattern1)) {
  29.                 output += el;
  30.             } else if (el.match(pattern2)) {
  31.                 weight += Number(el);
  32.             }
  33.         });
  34.         return [output, weight];
  35.     }
  36.  
  37.     function checkCountry(arg1) {
  38.         let output = '';
  39.         arg1 = arg1.split('');
  40.         let pattern1 = /[a-z\s]/gi;
  41.         arg1.forEach(el => {
  42.             if (el.match(pattern1)) {
  43.                 output += el;
  44.             }
  45.         });
  46.         return output;
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement