Advertisement
bebo231312312321

Untitled

Mar 20th, 2023
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(data) {
  2.   // transforming data parameter as a string
  3.   data = data.shift()
  4.   // get the pattern needed
  5.   let pattern = /(\||\#)(?<producyName>[A-Za-z]+(\s[A-Za-z]*)?)\1(?<date>\d{2}\/\d{2}\/\d{2})\1(?<calories>\d{1,9}|[10000])\1/gm
  6.   // will store total calorie and arrays with class matches so I can print it last
  7.   let totalCalorieCount = 0
  8.   let matchCounterName = []
  9.   let matchCounterDate = []
  10.   let matchCounterCalories = []
  11.   // get all matches that will be in the data
  12.   let matches = data.matchAll(pattern)
  13.   // go through the iterator of matches
  14.   for (const currentMatch of matches) {
  15.     let item = currentMatch.groups.producyName
  16.     let date = currentMatch.groups.date
  17.     let calories = currentMatch.groups.calories
  18.     matchCounterName.push(item)
  19.     matchCounterDate.push(date)
  20.     matchCounterCalories.push(calories)
  21.     totalCalorieCount += Number(currentMatch.groups.calories);
  22.  
  23.   }
  24.   //get 'how many days you can last with the food you have'
  25.   let daysToSurvive = Math.trunc(totalCalorieCount / 2000)
  26.   // log how many days you can last with the food you have and matches accordingly
  27.   console.log(`You have food to last you for: ${daysToSurvive} days!`);
  28.   for (let i = 0; i < matchCounterName.length; i++) {
  29.     console.log(`Item: ${matchCounterName[i]}, Best before: ${matchCounterDate[i]}, Nutrition: ${matchCounterCalories[i]}`);
  30.   }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement