audipower

animalSanctuary-Js-Fundamentals-FINAL-EXAM

Jul 25th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function animalSanctuary(input) {
  2.     let numOfLines = Number(input.shift());
  3.     let pattern = /n:(?<name>[^;]+);t:(?<kind>[^;]+);c--(?<country>[A-Za-z\s]+)/g;
  4.     let totalKilos = 0;
  5.  
  6.     for (let i = 0; i < numOfLines; i++) {
  7.         let line = input[i];
  8.         if (line.match(pattern)) {
  9.             let obj = pattern.exec(line);
  10.             let name = obj.groups.name.split(/[^a-z\s]+/i).join("");
  11.             let kind = obj.groups.kind.split(/[^a-z\s]+/i).join("");
  12.             let country = obj.groups.country;
  13.             let weight = obj.groups.name.concat(obj.groups.kind).split(/[^0-9]/g).filter((el) => el !== "")
  14.             weight = weight !== null ? totalKilos += weight.join("").split("").reduce((a, b) => Number(a) + Number(b), 0): totalKilos += 0;
  15.  
  16.             console.log(`${name} is a ${kind} from ${country}`);
  17.         }
  18.     }
  19.     console.log(`Total weight of animals: ${totalKilos}KG`)
  20. }
  21.  
  22. animalSanctuary([
  23.     "4",
  24.     "n:Bo^%4b35454bie#$;t:Ele5ph#$34a%nt;c--Africa",
  25.     "n:Honey;t:Ti^^5ger;c--India",
  26.     "bla;t:1234a;c--America",
  27.     "n:A#$@545n;t:Cat241$@#23;cGermany"
  28. ]);
  29.  
  30. // animalSanctuary([
  31. //     "3",
  32. //     "n:M5%ar4#le@y;t:B3#e!!a2#2r;c--Australia",
  33. //     "n:G3e%6org34e;t:C€$at2%;c--Africa",
  34. //     "n:AlicE:Won;c-India"
  35. // ]);
Advertisement
Add Comment
Please, Sign In to add comment