victoriaSD

zadacha 2 - exam prep

Mar 19th, 2024 (edited)
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function adAstra(input) {
  2.     let calsPerDay = 2000;
  3.     let totalCals = 0;
  4.     let arr = [];
  5.     let info = input.shift();
  6.     let regex =
  7.       /([#\|])(?<item>[A-Za-z\s]+)\1(?<date>[0-9]{2}\/[0-9]{2}\/[0-9]{2})\1(?<cals>\d{0,10000})\1/g;
  8.  
  9.     let match = regex.exec(info);
  10.  
  11.     while (match) {
  12.       arr.push(match[0]);
  13.       match = regex.exec(info);
  14.     }
  15.     for (let infoFood of arr) {
  16.       let [, itemName, expirationDate, calories] = infoFood.split(/[#|]/g);
  17.       calories = Number(calories);
  18.       totalCals += calories;
  19.     }
  20.  
  21.     let days = Math.floor(totalCals / calsPerDay);
  22.     console.log(`You have food to last you for: ${days} days!`);
  23.  
  24.     if (days > 0) {
  25.       for (let infoFood of arr) {
  26.           let [, itemName, expirationDate, calories] = infoFood.split(/[#|]/g);
  27.           console.log(
  28.             `Item: ${itemName}, Best before: ${expirationDate}, Nutrition: ${calories}`
  29.           );
  30.         }
  31.     }
  32.    
  33.   }
Advertisement
Add Comment
Please, Sign In to add comment