Advertisement
Pijomir

Ad Astra

Nov 25th, 2023
656
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function trackFoodInventory([input]) {
  2.     let itemsPattern = /([#|])(?<food>[A-Za-z ]+)\1(?<date>\d{2}\/\d{2}\/\d{2})\1(?<calories>\d{1,5})\1/g;
  3.     let currentItem = itemsPattern.exec(input);
  4.     let allCalories = 0;
  5.     let invetory = [];
  6.     while (currentItem) {
  7.         let { food, date, calories } = currentItem.groups;
  8.         calories = Number(calories);
  9.         allCalories += calories;
  10.         invetory.push({'name': food, 'date': date, 'calories': calories});
  11.         currentItem = itemsPattern.exec(input);
  12.     }
  13.  
  14.     let daysWithFood = Math.floor(allCalories / 2000);
  15.     console.log(`You have food to last you for: ${daysWithFood} days!`);
  16.     if (invetory.length > 0) {
  17.         invetory.forEach(product => console.log(`Item: ${product.name}, Best before: ${product.date}, Nutrition: ${product.calories}`));
  18.     }
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement