Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. function animalSanctuary(input) {
  2. let totalWeight = 0;
  3.  
  4. for (let line of input.slice(1)) {
  5.  
  6. let regex = /^n:(?<name>[^;]+);t:(?<kind>[^;]+);c--(?<country>[A-Za-z ]+)$/g;
  7. let match = regex.exec(line);
  8.  
  9. if (match !== null) {
  10. let weight = line.match(/[\d]/g);
  11. let { name, kind, country } = match.groups;
  12. let animalName = name.match(/[A-Za-z ]*/g);
  13. let animalKind = kind.match(/[A-Za-z ]*/g);
  14.  
  15. if (animalName !== null && animalKind !== null && country !== null) {
  16.  
  17. if (weight !== null) {
  18. weight = weight.map(Number).reduce((a, b) => a + b, 0);
  19. totalWeight += weight;
  20. }
  21.  
  22. console.log(`${animalName.join('')} is a ${animalKind.join('')} from ${country}`);
  23. }
  24. }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement