Advertisement
Guest User

Untitled

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