Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. animal = (arr) => {
  2. let count = arr.shift();
  3. let weightArr = [];
  4.  
  5. arr.forEach(element => {
  6. const patternName = new RegExp ("^n:[€£ -~]+$")
  7. const patternType = new RegExp ("^t:[€£ -~]+$", "gi")
  8. const patternCountry = new RegExp("^c--[ a-z]+$","gi")
  9. let [name, type, country] = element.split(';')
  10.  
  11. if(name.match(patternName) != null&& type.match(patternType) !== null && country.match(patternCountry) != null){
  12. element.split('').forEach(x => {
  13. if (!isNaN(x)){
  14. weightArr.push(x)
  15. }
  16. })
  17. name = name.split('').map(x => {
  18. if(x.match(new RegExp("[a-z ]","i"))){
  19. return x;
  20. }
  21. })
  22. type = type.split('').map(x => {
  23. if(x.match(new RegExp("[a-z ]","i"))){
  24. return x;
  25. }
  26. })
  27. country = country.split('').map(x => {
  28. if(x.match(new RegExp("[a-z ]","i"))){
  29. return x;
  30. }
  31. })
  32. name.shift()
  33. type.shift()
  34. country.shift()
  35. console.log(`${name.join('')} is a ${type.join('')} from ${country.join('')}`)
  36. }
  37. }
  38.  
  39. );
  40. let sum =0
  41. weightArr.forEach(element => {
  42. sum+=Number(element)
  43. });
  44. console.log(`Total weight of animals: ${sum}KG`)
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement