Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function animalSanctuary(input) {
- let countAnimals = input[0];
- let weight = 0;
- for (let numberAnimal = 1; numberAnimal <= countAnimals; numberAnimal++) {
- let pattern = /^n:{1}[^;]*;{1}t:{1}[^;]*;c--{1}[A-Za-z\s]+$/g;
- if (input[numberAnimal].match(pattern)) {
- let [animalName, animalKind, animalCountry] = input[numberAnimal].split(';');
- animalName = animalName.split('n:')[1];
- animalKind = animalKind.split('t:')[1];
- animalCountry = animalCountry.split('c--')[1];
- [name, weightName] = checkNameAndKind(animalName);
- [kind, weightKind] = checkNameAndKind(animalKind);
- weight += weightName + weightKind;
- country = checkCountry(animalCountry);
- console.log(`${name} is a ${kind} from ${country}`);
- }
- }
- console.log(`Total weight of animals: ${weight}KG`);
- function checkNameAndKind(arg1) {
- let output = '';
- let weight = 0
- arg1 = arg1.split('');
- let pattern1 = /[a-z\s]/gi;
- let pattern2 = /[0-9]/g;
- arg1.forEach(el => {
- if (el.match(pattern1)) {
- output += el;
- } else if (el.match(pattern2)) {
- weight += Number(el);
- }
- });
- return [output, weight];
- }
- function checkCountry(arg1) {
- let output = '';
- arg1 = arg1.split('');
- let pattern1 = /[a-z\s]/gi;
- arg1.forEach(el => {
- if (el.match(pattern1)) {
- output += el;
- }
- });
- return output;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement