Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function animalSanctuary(input) {
- const countAnimals = input.shift();
- let totalWeight = 0;
- for (let i = 1; i <= countAnimals; i++) {
- if ((line = (/n:(?<name>[^;]+);t:(?<kind>[^;]+);c--(?<country>[A-Za-z ]+)/g).exec(input.shift()))) {
- totalWeight += calculateWeight(line.groups['name'].concat(line.groups['kind']));
- console.log(`${extractNameAndKindAnimal(line.groups['name'])} is a ${extractNameAndKindAnimal(line.groups['kind'])} from ${line.groups['country']}`);
- }
- }
- console.log(`Total weight of animals: ${totalWeight}KG`);
- function extractNameAndKindAnimal(string) {
- return string
- .split('')
- .filter(char => {
- if (('a' <= char && char <= 'z') || ('A' <= char && char <= 'Z') || char === ' ')
- return true
- })
- .join('')
- }
- function calculateWeight(string) {
- return string.split('')
- .map(Number)
- .filter(char => char)
- .reduce((a, b) => a + b, 0)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement