Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let totalWeight = 0;
- for (let line of input) {
- let pattern = /^n:(?<animalName>[^;]+);t:(?<animalKind>[^;]+);c--(?<animalCountry>[A-Za-z\s]+)$/g;
- let result = pattern.exec(line);
- if (result) {
- let name = result.groups.animalName;
- let type = result.groups.animalKind;
- let country = result.groups.animalCountry;
- let animalName = '';
- let animalKind = '';
- let countryName = '';
- for (let i = 0; i < name.length; i++) {
- let currentChar = name[i];
- let ascii = currentChar.charCodeAt(0);
- if (ascii === 32 || (ascii >= 65 && ascii <= 90) || (ascii >= 97 && ascii <= 122)) {
- animalName += currentChar;
- }
- }
- for (let i = 0; i < type.length; i++) {
- let currentChar = type[i];
- let ascii = currentChar.charCodeAt(0);
- if (ascii === 32 || (ascii >= 65 && ascii <= 90) || (ascii >= 97 && ascii <= 122)) {
- animalKind += currentChar;
- }
- }
- for (let i = 0; i < country.length; i++) {
- let currentChar = country[i];
- let ascii = currentChar.charCodeAt(0);
- if (ascii === 32 || (ascii >= 65 && ascii <= 90) || (ascii >= 97 && ascii <= 122)) {
- countryName += currentChar;
- }
- }
- console.log(`${animalName} is a ${animalKind} from ${countryName}`);
- let charsForWeight = name + type;
- if (!charsForWeight.match(/\d/g)) {
- totalWeight += 0;
- } else {
- totalWeight += charsForWeight.match(/\d/g).map(Number).reduce((a, b) => a + b, 0);
- }
- }
- }
- console.log(`Total weight of animals: ${totalWeight}KG`);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement