Advertisement
Spocoman

02. Ad Astra

Nov 28th, 2023 (edited)
707
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function adAstra(input) {
  2.     let maches = input[0].match(/([|#])([A-Za-z\s]+)\1(\d{2}\/\d{2}\/\d{2})\1(\d{1,5})\1/g);
  3.     let products = [];
  4.     let sumCalories = 0;
  5.  
  6.     if (maches) {
  7.         for (let match of maches) {
  8.             let m = match.split(match.includes("#") ? "#" : "|").filter(x => x !== "");
  9.             products.push({ name: m[0], date: m[1], calories: m[2] });
  10.             sumCalories += Number(m[2]);
  11.         }
  12.     }
  13.  
  14.     console.log(`You have food to last you for: ${Math.floor(sumCalories / 2000)} days!`);
  15.  
  16.     for (let p of products) {
  17.         console.log(`Item: ${p.name}, Best before: ${p.date}, Nutrition: ${p.calories}`);
  18.     }
  19.    
  20.     return;
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement