Advertisement
ErolKZ

Untitled

Nov 26th, 2021
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. function solve(input) {
  2.  
  3.  
  4. 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;
  5.  
  6. let arr = [];
  7.  
  8. let totalCalories = 0;
  9.  
  10. let days = 0;
  11.  
  12. let obj = {};
  13.  
  14.  
  15. while ((food = pattern.exec(input[0])) !== null) {
  16.  
  17. if (food.groups.item !== undefined) {
  18.  
  19. let item = food.groups.item;
  20.  
  21. let date = food.groups.date;
  22.  
  23. let calories = food.groups.calories;
  24.  
  25. totalCalories += Number(calories);
  26.  
  27. obj = { item: item, date: date, calories: calories };
  28.  
  29. arr.push(obj);
  30.  
  31.  
  32. } else {
  33.  
  34. let item = food.groups.item2;
  35.  
  36. let date = food.groups.date2;
  37.  
  38. let calories = food.groups.calories2;
  39.  
  40. totalCalories += Number(calories);
  41.  
  42. obj = { item: item, date: date, calories: calories };
  43.  
  44. arr.push(obj);
  45.  
  46. }
  47.  
  48. }
  49.  
  50.  
  51.  
  52. days = totalCalories / 2000;
  53.  
  54. days = Math.floor(days);
  55.  
  56. console.log(`You have food to last you for: ${days} days!`);
  57.  
  58. if (days > 0) {
  59.  
  60. for (let el of arr) {
  61.  
  62. console.log(`Item: ${el['item']}, Best before: ${el['date']}, Nutrition: ${el['calories']}`);
  63.  
  64. }
  65.  
  66. }
  67.  
  68.  
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement