Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(data) {
- // transforming data parameter as a string
- data = data.shift()
- // get the pattern needed
- 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
- // will store total calorie and arrays with class matches so I can print it last
- let totalCalorieCount = 0
- let matchCounterName = []
- let matchCounterDate = []
- let matchCounterCalories = []
- // get all matches that will be in the data
- let matches = data.matchAll(pattern)
- // go through the iterator of matches
- for (const currentMatch of matches) {
- let item = currentMatch.groups.producyName
- let date = currentMatch.groups.date
- let calories = currentMatch.groups.calories
- matchCounterName.push(item)
- matchCounterDate.push(date)
- matchCounterCalories.push(calories)
- totalCalorieCount += Number(currentMatch.groups.calories);
- }
- //get 'how many days you can last with the food you have'
- let daysToSurvive = Math.trunc(totalCalorieCount / 2000)
- // log how many days you can last with the food you have and matches accordingly
- console.log(`You have food to last you for: ${daysToSurvive} days!`);
- for (let i = 0; i < matchCounterName.length; i++) {
- console.log(`Item: ${matchCounterName[i]}, Best before: ${matchCounterDate[i]}, Nutrition: ${matchCounterCalories[i]}`);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement