Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 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. const letters = (word =>{
  18. let result = [];
  19. word.split('').map(x => {
  20. if(x.match(new RegExp("[a-z ]","i"))){
  21. result.push(x)
  22. }
  23. })
  24. result.shift()
  25. return result.join('')
  26. })
  27. name = letters(name)
  28. type = letters(type)
  29. country = letters(country)
  30. console.log(`${name} is a ${type} from ${country}`)
  31. }
  32. }
  33. );
  34. let sum =0
  35. weightArr.forEach(element => {
  36. sum+=Number(element)
  37. });
  38. console.log(`Total weight of animals: ${sum}KG`)
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement