Advertisement
Lulunga

final exam 01. Animal Sanctuary

Aug 1st, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.    let totalWeight = 0;
  3.  
  4.    for (let line of input) {
  5.       let pattern = /^n:(?<animalName>[^;]+);t:(?<animalKind>[^;]+);c--(?<animalCountry>[A-Za-z\s]+)$/g;
  6.       let result = pattern.exec(line);
  7.       if (result) {
  8.  
  9.          let name = result.groups.animalName;
  10.          let type = result.groups.animalKind;
  11.          let country = result.groups.animalCountry;
  12.  
  13.          let animalName = '';
  14.          let animalKind = '';
  15.          let countryName = '';
  16.  
  17.          for (let i = 0; i < name.length; i++) {
  18.             let currentChar = name[i];
  19.             let ascii = currentChar.charCodeAt(0);
  20.             if (ascii === 32 || (ascii >= 65 && ascii <= 90) || (ascii >= 97 && ascii <= 122)) {
  21.                animalName += currentChar;
  22.             }
  23.          }
  24.  
  25.          for (let i = 0; i < type.length; i++) {
  26.             let currentChar = type[i];
  27.             let ascii = currentChar.charCodeAt(0);
  28.             if (ascii === 32 || (ascii >= 65 && ascii <= 90) || (ascii >= 97 && ascii <= 122)) {
  29.                animalKind += currentChar;
  30.             }
  31.          }
  32.  
  33.          for (let i = 0; i < country.length; i++) {
  34.             let currentChar = country[i];
  35.             let ascii = currentChar.charCodeAt(0);
  36.             if (ascii === 32 || (ascii >= 65 && ascii <= 90) || (ascii >= 97 && ascii <= 122)) {
  37.                countryName += currentChar;
  38.             }
  39.          }
  40.          console.log(`${animalName} is a ${animalKind} from ${countryName}`);
  41.  
  42.          let charsForWeight = name + type;
  43.          if (!charsForWeight.match(/\d/g)) {
  44.             totalWeight += 0;
  45.          } else {
  46.             totalWeight += charsForWeight.match(/\d/g).map(Number).reduce((a, b) => a + b, 0);
  47.  
  48.          }
  49.       }
  50.    }
  51.    console.log(`Total weight of animals: ${totalWeight}KG`);
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement