Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function animalSanctuary(input) {
- let numOfLines = Number(input.shift());
- let pattern = /n:(?<name>[^;]+);t:(?<kind>[^;]+);c--(?<country>[A-Za-z\s]+)/g;
- let totalKilos = 0;
- for (let i = 0; i < numOfLines; i++) {
- let line = input[i];
- if (line.match(pattern)) {
- let obj = pattern.exec(line);
- let name = obj.groups.name.split(/[^a-z\s]+/i).join("");
- let kind = obj.groups.kind.split(/[^a-z\s]+/i).join("");
- let country = obj.groups.country;
- let weight = obj.groups.name.concat(obj.groups.kind).split(/[^0-9]/g).filter((el) => el !== "")
- weight = weight !== null ? totalKilos += weight.join("").split("").reduce((a, b) => Number(a) + Number(b), 0): totalKilos += 0;
- console.log(`${name} is a ${kind} from ${country}`);
- }
- }
- console.log(`Total weight of animals: ${totalKilos}KG`)
- }
- animalSanctuary([
- "4",
- "n:Bo^%4b35454bie#$;t:Ele5ph#$34a%nt;c--Africa",
- "n:Honey;t:Ti^^5ger;c--India",
- "bla;t:1234a;c--America",
- "n:A#$@545n;t:Cat241$@#23;cGermany"
- ]);
- // animalSanctuary([
- // "3",
- // "n:M5%ar4#le@y;t:B3#e!!a2#2r;c--Australia",
- // "n:G3e%6org34e;t:Cā¬$at2%;c--Africa",
- // "n:AlicE:Won;c-India"
- // ]);
Advertisement
Add Comment
Please, Sign In to add comment