Advertisement
bebo231312312321

Untitled

Mar 20th, 2023
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. function adAstra(array) {
  3.     let pattern =
  4.         /(\#|\|)(?<item>[A-Za-z\s]+)(\1)(?<date>\d{1,2}\/\d{1,2}\/\d{1,2})(\1)(?<calories>\d{1,5})(\1)/g;
  5.     // /(?<separate>#|\|)(?<item>[A-Za-z ]+)\1(?<expDate>\d{2}\/\d{2}\/\d{2})\1(?<calories>\d+)/g;
  6.     let result = []
  7.     let sum = 0
  8.     let match = pattern.exec(array[0])
  9.  
  10.     while (match !== null) {
  11.         let date = match.groups.date
  12.         let calories = match.groups.calories
  13.         let items = match.groups.items
  14.         sum += Number(calories)
  15.         result.push([items, date, calories])
  16.  
  17.         match = pattern.exec(array[0])
  18.     }
  19.     let days = Math.floor(sum/2000)
  20.     console.log(`You have food to last you for: ${days} days!`)
  21.   result.forEach(el=>
  22.         console.log(`Item: ${el[0]}, Best before: ${el[1]}, Nutrition: ${el[2]}`));
  23.  
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement