Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let pattern = /\#(?<item>[A-Za-z ]+)\#(?<date>\d{2}\/\d{2}\/\d{2})\#(?<calories>[0-9]{0,4}(?=\#)|[1][0]{4}(?=\#))\#|\|(?<item2>[A-Za-z ]+)\|(?<date2>\d{2}\/\d{2}\/\d{2})\|(?<calories2>[0-9]{0,4}(?=\|)|[1][0]{4}(?=\|))\|/gm;
- let arr = [];
- let totalCalories = 0;
- let days = 0;
- let obj = {};
- while ((food = pattern.exec(input[0])) !== null) {
- if (food.groups.item !== undefined) {
- let item = food.groups.item;
- let date = food.groups.date;
- let calories = food.groups.calories;
- totalCalories += Number(calories);
- obj = { item: item, date: date, calories: calories };
- arr.push(obj);
- } else {
- let item = food.groups.item2;
- let date = food.groups.date2;
- let calories = food.groups.calories2;
- totalCalories += Number(calories);
- obj = { item: item, date: date, calories: calories };
- arr.push(obj);
- }
- }
- days = totalCalories / 2000;
- days = Math.floor(days);
- console.log(`You have food to last you for: ${days} days!`);
- if (days > 0) {
- for (let el of arr) {
- console.log(`Item: ${el['item']}, Best before: ${el['date']}, Nutrition: ${el['calories']}`);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement